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