5 people like it.

Idiomatic F# wrapper for storing and reading from MonoDB

Basic wrapper for storing and reading documents from MongoDB with the official .NET driver.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
open MongoDB.Bson
open MongoDB.Bson.IO
open MongoDB.Driver

/// <summary>
/// Contains functions for communicating with MongoDB.
/// </summary>
module Mongo =
    /// <summary>
    /// Stores a document in the MongoDB target.
    /// </summary>
    let store (connection : string) database collection document =
        let client = new MongoClient(connection)
        let db = database |> client.GetDatabase
        let collection = collection |> db.GetCollection
        let document = document |> BsonDocument.Parse
        document |> collection.InsertOne
        let settings = JsonWriterSettings()
        settings.OutputMode <- JsonOutputMode.Strict
        let document = document.ToJson(settings)
        document

    /// <summary>
    /// Reads a document or documents from the MongoDB target.
    /// </summary>
    let read (connection : string) database collection (filter : string option) =
        let client = new MongoClient(connection)
        let db = database |> client.GetDatabase
        let collection = collection |> db.GetCollection<BsonDocument>
        let cursor : IAsyncCursor<BsonDocument> =
            collection.FindSync(
                filter
                |> Option.map (BsonDocument.Parse >> FilterDefinition.op_Implicit)
                |> Option.defaultValue FilterDefinition.Empty)
        let settings = JsonWriterSettings()
        settings.OutputMode <- JsonOutputMode.Strict
        let rec readDoc docs =
            if cursor.MoveNext() |> not then
                docs
            else
                let batch = cursor.Current
                batch
                |> Seq.toArray
                |> Array.map (fun d -> d.ToJson(settings))
                |> Array.append docs
                |> readDoc
        readDoc [|  |]
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
module Option

from Microsoft.FSharp.Core
val map : mapping:('T -> 'U) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.map
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
module Seq

from Microsoft.FSharp.Collections
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val append : array1:'T [] -> array2:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.append
Raw view Test code New version

More information

Link:http://fssnip.net/7Xm
Posted:5 years ago
Author:Elliot V. Brown
Tags: database , f# , mongodb , wrapper