MongoDB F# Driver
Max Hirschhorn (@visemet)
github.com/10gen-interns/mongo-fsharp-driver-prototype
Outline
1. Brief overview of MongoDB
2. Introduction to F# language
3. Structure of F# + .NET drivers
4. Installing the dr...
MongoDB Overview
● Popular NoSQL database
● Stores BSON, binary-form of JSON
● Interact with (query, etc.) using
JSON
What is F#?
● Multi-paradigm (functional, OO, imperative)
● Statically-typed + type inference system
● Compatible with oth...
Functions and types
Same primitives as with .NET
Able to curry parameters and partially
apply functions
// int -> int -> i...
// Definition
// 'a -> ('a -> 'b) -> 'b
let (|>) x f = f x
// Usage
100 |> inc
|> double
// ~~> 202
|> (forward pipe) oper...
Refactored into a separate Core.dll to
provide low-level operations
Structure of .NET driver
Core BSON
F# high-
level
C# h...
Installation
Not available on NuGet (yet)
1. Clone from GitHub
2. Build solution
3. Add references to project
Connecting
Maintains naming convention
let client = // localhost:27017
MongoClient() :> IMongoClient
let db = client.GetDa...
Inserts
let doc =
BsonDocument("foo", BsonInt32(1))
items.Insert doc
Supports (automatic) serialization of
● record types
...
Deletes
Chain together operations with
predicate
let query = ...
items.Find(query)
|> Scope.remove
Queries before (1)
C#
new BsonDocument {
{ "price", 1.99 },
{ "$or", new BsonDocument {
{ "qty", new BsonDocument { { "$lt...
Queries before (2)
Query builder
Query.And([ Query.EQ("price", BsonDouble(1.99))
Query.OR([ Query.LT("qty", BsonInt32(20))...
Collection Schema
Use cases for query/update operations
against a collection
1. Unknown schema
2. Given schema, typed defi...
? (dynamic get) operator
Performs dictionary lookup
doc?foo // ~~> doc.["foo"] ok
doc?bar // ~~> doc.["bar"] fails
Queries after
Now able to be written using
● code quotations
● computation expressions
Reuse built-in operators wherever
p...
Type-checked
<@ fun (x : Item) ->
x.price = 1.99 && (x.qty < 20 || x.sale = true) @>
|> bson
and -unchecked versions
<@ fu...
mongo expressions
Collection can be typed or untyped
mongo {
for x in items do
where (x.price = 1.99
&& (x.qty < 20 || x.s...
?<- (dynamic set) operator (2)
Performs dictionary assignment
doc?foo <- 0
// ~~> doc.["foo"] = 0 ok
doc?bar <- 0
// ~~> d...
Updates
Again able to be written using
● code quotations
● computation expressions
Reuse built-in operators wherever
possi...
bson quotations (1)
Type-checked version
Immutable fields
<@ fun (x : Item) ->
{ x with price = 0.99; qty = x.qty + 10 } @...
bson quotations (2)
Type-unchecked version
Sugared
<@ fun (x : BsonDocument) ->
[ x?price <- 0.99; x?qty <- (+) 10 ] @>
|>...
mongo expressions
Collection can be typed or untyped
mongo {
for x in items do
update
set x.price 0.99
inc x.qty 10
}
Questions?
Special thanks to…
Sridhar Nanjundeswaran
Craig Wilson
Robert Stam
Mathias Brandewinder
Don Syme
Keith Battocch...
Record Serialization (1)
Set of named values
// Example definition
type Item = {
price : float
qty : int
sale : bool
}
Record Serialization (2)
// Example usage
let item = { price = 1.99
qty = 20
sale = true }
// ~~> serialized as JSON
// { ...
Union Serialization (1)
Discriminated union is a series of cases
of (potentially) varying arity and types
type Number =
| ...
Union Serialization (2)
let zero = Zero
let one = One 1
let two = Two (2, "two")
// ~~> serialized as JSON
// { _t: "Zero"...
Option Serialization
Haskell-inspired equivalent of
Just/Nothing
Serialize the enclosed value when Some
case, and omit whe...
Prochain SlideShare
Chargement dans…5
×

The MongoDB Driver for F#

2 627 vues

Publié le

Publié dans : Technologie
  • Soyez le premier à commenter

The MongoDB Driver for F#

  1. 1. MongoDB F# Driver Max Hirschhorn (@visemet) github.com/10gen-interns/mongo-fsharp-driver-prototype
  2. 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. 3. MongoDB Overview ● Popular NoSQL database ● Stores BSON, binary-form of JSON ● Interact with (query, etc.) using JSON
  4. 4. What is F#? ● Multi-paradigm (functional, OO, imperative) ● Statically-typed + type inference system ● Compatible with other .NET languages ● Syntax similar to Ocaml
  5. 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. 6. // Definition // 'a -> ('a -> 'b) -> 'b let (|>) x f = f x // Usage 100 |> inc |> double // ~~> 202 |> (forward pipe) operator
  7. 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. 8. Installation Not available on NuGet (yet) 1. Clone from GitHub 2. Build solution 3. Add references to project
  9. 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. 10. Inserts let doc = BsonDocument("foo", BsonInt32(1)) items.Insert doc Supports (automatic) serialization of ● record types ● discriminated unions ● option types
  11. 11. Deletes Chain together operations with predicate let query = ... items.Find(query) |> Scope.remove
  12. 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. 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. 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. 15. ? (dynamic get) operator Performs dictionary lookup doc?foo // ~~> doc.["foo"] ok doc?bar // ~~> doc.["bar"] fails
  16. 16. Queries after Now able to be written using ● code quotations ● computation expressions Reuse built-in operators wherever possible
  17. 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. 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. 19. ?<- (dynamic set) operator (2) Performs dictionary assignment doc?foo <- 0 // ~~> doc.["foo"] = 0 ok doc?bar <- 0 // ~~> doc.["bar"] = 0 fails
  20. 20. Updates Again able to be written using ● code quotations ● computation expressions Reuse built-in operators wherever possible
  21. 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. 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. 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. 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. 25. Record Serialization (1) Set of named values // Example definition type Item = { price : float qty : int sale : bool }
  26. 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. 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. 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. 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

×