Skip to content

Instantly share code, notes, and snippets.

@naartjie
Created November 29, 2020 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naartjie/a6a1f369d98a7c101f7643e6c70a439a to your computer and use it in GitHub Desktop.
Save naartjie/a6a1f369d98a7c101f7643e6c70a439a to your computer and use it in GitHub Desktop.
F# MongoDB Starter
dotnet new console -lang F# -o MyMongoApp
dotnet add package MongoDB.Driver --version 2.11.4
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment