Your SlideShare is downloading. ×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

The MongoDB Driver for F#

1,243
views

Published on

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,243
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. MongoDB F# Driver Max Hirschhorn (@visemet) github.com/10gen-interns/mongo-fsharp-driver-prototype
  • 2. Outline 1. Brief overview of MongoDB 2. Introduction to F# language 3. Structure of F# + .NET drivers 4. Installing the driver 5. Connecting to the server 6. Executing operations
  • 3. MongoDB Overview ● Popular NoSQL database ● Stores BSON, binary-form of JSON ● Interact with (query, etc.) using JSON
  • 4. What is F#? ● Multi-paradigm (functional, OO, imperative) ● Statically-typed + type inference system ● Compatible with other .NET languages ● Syntax similar to Ocaml
  • 5. Functions and types Same primitives as with .NET Able to curry parameters and partially apply functions // int -> int -> int let add x y = x + y // int -> int let inc = add 1// int -> int let double x = add x x
  • 6. // Definition // 'a -> ('a -> 'b) -> 'b let (|>) x f = f x // Usage 100 |> inc |> double // ~~> 202 |> (forward pipe) operator
  • 7. Refactored into a separate Core.dll to provide low-level operations Structure of .NET driver Core BSON F# high- level C# high- level
  • 8. Installation Not available on NuGet (yet) 1. Clone from GitHub 2. Build solution 3. Add references to project
  • 9. Connecting Maintains naming convention let client = // localhost:27017 MongoClient() :> IMongoClient let db = client.GetDatabase "test" let items = db.GetCollection "items" - or - let items = db.GetCollection<'DocType> "items"
  • 10. Inserts let doc = BsonDocument("foo", BsonInt32(1)) items.Insert doc Supports (automatic) serialization of ● record types ● discriminated unions ● option types
  • 11. Deletes Chain together operations with predicate let query = ... items.Find(query) |> Scope.remove
  • 12. Queries before (1) C# new BsonDocument { { "price", 1.99 }, { "$or", new BsonDocument { { "qty", new BsonDocument { { "$lt", 20 } } }, { "sale", true } } } }; F# BsonDocument([ BsonElement("price", BsonDouble(1.99)) BsonElement("$or", BsonArray([ ... ])) ])
  • 13. Queries before (2) Query builder Query.And([ Query.EQ("price", BsonDouble(1.99)) Query.OR([ Query.LT("qty", BsonInt32(20)) Query.EQ("sale", BsonBoolean(true)) ]) ]) Typed query builder let query = QueryBuilder<Item>() query.And([ query.EQ((fun x -> x.price), 1.99) query.OR([ query.LT((fun x -> x.qty), 20) query.EQ((fun x -> x.sale), true) ]) ])
  • 14. Collection Schema Use cases for query/update operations against a collection 1. Unknown schema 2. Given schema, typed defined by developer 3. Inferred schema, where operations are checked at compile-time
  • 15. ? (dynamic get) operator Performs dictionary lookup doc?foo // ~~> doc.["foo"] ok doc?bar // ~~> doc.["bar"] fails
  • 16. Queries after Now able to be written using ● code quotations ● computation expressions Reuse built-in operators wherever possible
  • 17. Type-checked <@ fun (x : Item) -> x.price = 1.99 && (x.qty < 20 || x.sale = true) @> |> bson and -unchecked versions <@ fun (x : BsonDocument) -> x?price = 1.99 && (x?qty < 20 || x?sale = true) @> |> bson bson quotations
  • 18. mongo expressions Collection can be typed or untyped mongo { for x in items do where (x.price = 1.99 && (x.qty < 20 || x.sale = true)) }
  • 19. ?<- (dynamic set) operator (2) Performs dictionary assignment doc?foo <- 0 // ~~> doc.["foo"] = 0 ok doc?bar <- 0 // ~~> doc.["bar"] = 0 fails
  • 20. Updates Again able to be written using ● code quotations ● computation expressions Reuse built-in operators wherever possible
  • 21. bson quotations (1) Type-checked version Immutable fields <@ fun (x : Item) -> { x with price = 0.99; qty = x.qty + 10 } @> |> bson Mutable fields <@ fun (x : Item) -> [ x.price <- 0.99; x.qty <- x.qty + 10 ] @> |> bson
  • 22. bson quotations (2) Type-unchecked version Sugared <@ fun (x : BsonDocument) -> [ x?price <- 0.99; x?qty <- (+) 10 ] @> |> bson Unsugared <@ fun (x : BsonDocument) -> [ x?price <- 0.99; x?qty <- x?qty + 10 ] @> |> bson
  • 23. mongo expressions Collection can be typed or untyped mongo { for x in items do update set x.price 0.99 inc x.qty 10 }
  • 24. Questions? Special thanks to… Sridhar Nanjundeswaran Craig Wilson Robert Stam Mathias Brandewinder Don Syme Keith Battocchi Tomas Petricek … and the F# community on Twitter
  • 25. Record Serialization (1) Set of named values // Example definition type Item = { price : float qty : int sale : bool }
  • 26. Record Serialization (2) // Example usage let item = { price = 1.99 qty = 20 sale = true } // ~~> serialized as JSON // { price: 1.99 // , qty: 20 // , sale: true }
  • 27. Union Serialization (1) Discriminated union is a series of cases of (potentially) varying arity and types type Number = | Zero | One of int | Two of int * string
  • 28. Union Serialization (2) let zero = Zero let one = One 1 let two = Two (2, "two") // ~~> serialized as JSON // { _t: "Zero" } // { _t: "One", Item: 1 } // { _t: "Two" // , Item1: 2 // , Item2: "two" }
  • 29. Option Serialization Haskell-inspired equivalent of Just/Nothing Serialize the enclosed value when Some case, and omit when None case Used to represent heterogeneous documents within collection