+ All Categories
Home > Technology > The MongoDB Driver for F#

The MongoDB Driver for F#

Date post: 05-Dec-2014
Category:
Upload: mongodb
View: 2,050 times
Download: 2 times
Share this document with a friend
Description:
 
29
MongoDB F# Driver Max Hirschhorn (@visemet) github.com/10gen-interns/mongo-fsharp-driver-prototype
Transcript
Page 1: The MongoDB Driver for F#

MongoDB F# DriverMax Hirschhorn (@visemet)github.com/10gen-interns/mongo-fsharp-driver-prototype

Page 2: The MongoDB Driver for F#

Outline1. 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

Page 3: The MongoDB Driver for F#

MongoDB Overview● Popular NoSQL database

● Stores BSON, binary-form of JSON

● Interact with (query, etc.) using JSON

Page 4: The MongoDB Driver for F#

What is F#?● Multi-paradigm (functional, OO, imperative)

● Statically-typed + type inference system

● Compatible with other .NET languages

● Syntax similar to Ocaml

Page 5: The MongoDB Driver for F#

Functions and typesSame primitives as with .NET

Able to curry parameters and partially apply functions

// int -> int -> intlet add x y = x + y

// int -> intlet inc = add 1// int -> int

let double x = add x x

Page 6: The MongoDB Driver for F#

// Definition// 'a -> ('a -> 'b) -> 'blet (|>) x f = f x

// Usage100 |> inc |> double// ~~> 202

|> (forward pipe) operator

Page 7: The MongoDB Driver for F#

Refactored into a separate Core.dll to provide low-level operations

Structure of .NET driver

Core BSON

F# high- level

C# high- level

Page 8: The MongoDB Driver for F#

InstallationNot available on NuGet (yet)

1. Clone from GitHub2. Build solution3. Add references to project

Page 9: The MongoDB Driver for F#

ConnectingMaintains naming convention

let client = // localhost:27017MongoClient() :> IMongoClient

let db = client.GetDatabase "test"

let items = db.GetCollection "items"

- or -

let items = db.GetCollection<'DocType> "items"

Page 10: The MongoDB Driver for F#

Insertslet doc =BsonDocument("foo", BsonInt32(1))

items.Insert doc

Supports (automatic) serialization of● record types● discriminated unions● option types

Page 11: The MongoDB Driver for F#

DeletesChain together operations with predicate

let query = ...items.Find(query)|> Scope.remove

Page 12: The MongoDB Driver for F#

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([ ... ])) ])

Page 13: The MongoDB Driver for F#

Queries before (2)Query builderQuery.And([ Query.EQ("price", BsonDouble(1.99)) Query.OR([ Query.LT("qty", BsonInt32(20)) Query.EQ("sale", BsonBoolean(true)) ]) ])

Typed query builderlet 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) ]) ])

Page 14: The MongoDB Driver for F#

Collection SchemaUse cases for query/update operations against a collection1. Unknown schema

2. Given schema, typed defined by developer

3. Inferred schema, where operations are checked at compile-time

Page 15: The MongoDB Driver for F#

? (dynamic get) operator

Performs dictionary lookup

doc?foo // ~~> doc.["foo"] okdoc?bar // ~~> doc.["bar"] fails

Page 16: The MongoDB Driver for F#

Queries after

Now able to be written using● code quotations● computation expressions

Reuse built-in operators wherever possible

Page 17: The MongoDB Driver for F#

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

Page 18: The MongoDB Driver for F#

mongo expressionsCollection can be typed or untyped

mongo {for x in items dowhere (x.price = 1.99

&& (x.qty < 20 || x.sale = true))

}

Page 19: The MongoDB Driver for F#

?<- (dynamic set) operator (2)

Performs dictionary assignment

doc?foo <- 0// ~~> doc.["foo"] = 0 ok

doc?bar <- 0// ~~> doc.["bar"] = 0 fails

Page 20: The MongoDB Driver for F#

UpdatesAgain able to be written using

● code quotations● computation expressions

Reuse built-in operators wherever possible

Page 21: The MongoDB Driver for F#

bson quotations (1)Type-checked versionImmutable 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

Page 22: The MongoDB Driver for F#

bson quotations (2)Type-unchecked versionSugared<@ 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

Page 23: The MongoDB Driver for F#

mongo expressionsCollection can be typed or untyped

mongo {for x in items doupdateset x.price 0.99inc x.qty 10

}

Page 24: The MongoDB Driver for F#

Questions?

Special thanks to…Sridhar NanjundeswaranCraig WilsonRobert StamMathias BrandewinderDon SymeKeith BattocchiTomas Petricek

… and the F# community on Twitter

Page 25: The MongoDB Driver for F#

Record Serialization (1)Set of named values

// Example definitiontype Item = {price : floatqty : intsale : bool

}

Page 26: The MongoDB Driver for F#

Record Serialization (2)// Example usagelet item = { price = 1.99 qty = 20 sale = true }

// ~~> serialized as JSON// { price: 1.99// , qty: 20// , sale: true }

Page 27: The MongoDB Driver for F#

Union Serialization (1)Discriminated union is a series of casesof (potentially) varying arity and types

type Number = | Zero | One of int | Two of int * string

Page 28: The MongoDB Driver for F#

Union Serialization (2)let zero = Zerolet one = One 1let two = Two (2, "two")

// ~~> serialized as JSON// { _t: "Zero" }// { _t: "One", Item: 1 }// { _t: "Two"// , Item1: 2// , Item2: "two" }

Page 29: The MongoDB Driver for F#

Option SerializationHaskell-inspired equivalent of Just/Nothing

Serialize the enclosed value when Some case, and omit when None case

Used to represent heterogeneous documents within collection


Recommended