Skip to content

Instantly share code, notes, and snippets.

@naartjie
Created November 29, 2020 23:13
Show Gist options
  • Save naartjie/a6a1f369d98a7c101f7643e6c70a439a to your computer and use it in GitHub Desktop.
Save naartjie/a6a1f369d98a7c101f7643e6c70a439a to your computer and use it in GitHub Desktop.

Revisions

  1. naartjie created this gist Nov 29, 2020.
    31 changes: 31 additions & 0 deletions App.fs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    open MongoDB.Driver
    open MongoDB.Bson
    open MongoDB.Driver.Linq

    let doMongoStuff () =

    let client = MongoClient("mongodb://localhost:27017")
    let db = client.GetDatabase("rebujito-test")

    let collectionName = "users"
    let collection = db.GetCollection<BsonDocument>(collectionName)

    let count = collection.CountDocuments(FilterDefinitionBuilder().Empty);
    printfn "we've got %d elements in %s" count collectionName

    async {
    printfn "starting async workflow..."

    let! count = collection.AsQueryable<BsonDocument>().CountAsync() |> Async.AwaitTask
    printfn "we've got %d elements in %s" count collectionName

    let! users = collection.AsQueryable<BsonDocument>().Where(fun x -> true).Select(fun x -> x).ToListAsync() |> Async.AwaitTask
    printfn "we got %d users" users.Count

    } |> Async.RunSynchronously

    [<EntryPoint>]
    let main _argv =
    printfn "Hello Mongo"
    doMongoStuff ()
    0
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    ```sh
    dotnet new console -lang F# -o MyMongoApp
    dotnet add package MongoDB.Driver --version 2.11.4
    ```