Mondocks 0.1.0-beta2

A CE library for F# that may provide better interactions with mongo databases

This is a prerelease version of Mondocks.
There is a newer version of this package available.
See the version list below for details.
Install-Package Mondocks -Version 0.1.0-beta2
dotnet add package Mondocks --version 0.1.0-beta2
<PackageReference Include="Mondocks" Version="0.1.0-beta2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mondocks --version 0.1.0-beta2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Mondocks, 0.1.0-beta2"
For F# scripts that support #r syntax, copy this into the source code to reference the package.

Mondocks

A CE library to ease your work with mongodb from F#

This library is based on the mongodb manual reference
https://docs.mongodb.com/manual/reference/command/

the mongodb .NET driver is made entirely for C# and it expects you to pass data and information in a way C# can (reflection, inheritance among others) so I tried to go in a different way providing a kind of DSL that allow you to create json files leveraging the dynamism of anonymous records since they behave almost like javascript objects.

Writing commands should be almost painless these commands produce a JSON string that can be utilized directly on your application or even copy/pasted into the mongo shell

Commands are kind of a version of raw sql queries but they allow you to do what you already know how to do without much changes to the objects you might be manipulating already

Sample Usage

Check out this quick sample of the work so far

open System
open MongoDB.Driver
open MongoDB.Bson
open Mondocks.Queries
open Mondocks.Types

let createUsers minAge maxAge = 
    let random  = Random()
    insert "users" {
        documents 
            [
                {| name = "Peter"; age = random.Next(minAge, maxAge); |}
                {| name = "Sandra"; age = random.Next(minAge, maxAge); |}
                {| name = "Mike"; age = random.Next(minAge, maxAge); |}
                {| name = "Perla"; age = random.Next(minAge, maxAge); |}
                {| name = "Updateme"; age = 1; |}
                {| name = "Deleteme"; age = 50; |}
            ]
    }

let updateUser (name: string) (newName: string) =
    update "users" {
        updates
            [
                { q = {| name = name |} 
                  u = {| name = newName; age = 5 |}
                  multi = Some false
                  upsert = Some false
                  collation = None 
                  arrayFilters = None
                  hint = None }
            ]
    }


let deleteUser (name: string) = 
    delete "users" {
        deletes [
            { q = {| name = name |}
              limit = 1
              collation = None
              hint = None
              comment = None }
        ]
    }

// Define a function to construct a message to print
let getUsersOverAge (age: int) =
    find "users" {
        filter {| age = {| ``$gt``= age |} |}
        limit 2
        skip 1
    }
type User = { _id: ObjectId; name: string; age: int }
[<EntryPoint>]
let main argv =
    let client = MongoClient("mongodb://localhost:27017")
    let db = client.GetDatabase("mondocks")

    let userscmd = createUsers 15 50
    let result = db.RunCommand<InsertResult>(JsonCommand userscmd)
    printfn $"InsertResult: %A{result}"

    let over20 = getUsersOverAge 20
    let result = db.RunCommand<FindResult<User>>(JsonCommand over20)
    printfn $"FindResult Ok: %d{result.ok}"
    result.cursor.firstBatch |> Seq.iter (fun value -> printfn $"%A{value}")

    let updatecmd = updateUser "Updateme" "Updated"
    let result = db.RunCommand<UpdateResult>(JsonCommand updatecmd)
    printfn $"UpdateResult: %A{result}"

    let deletecmd = deleteUser "Deleteme"
    let result = db.RunCommand<DeleteResult>(JsonCommand deletecmd)
    printfn $"DeleteResult: %A{result}"
    
    0 // return an integer exit code// return an integer exit code

Thanks for the early feedback in twitter from Isaac, Zaid, Alexey, Alexander, and the F# community
you can follow it on the first issue

WIP

This is a work in progress, you can help providing feedback about it's usage

Samples

Mondocks

A CE library to ease your work with mongodb from F#

This library is based on the mongodb manual reference
https://docs.mongodb.com/manual/reference/command/

the mongodb .NET driver is made entirely for C# and it expects you to pass data and information in a way C# can (reflection, inheritance among others) so I tried to go in a different way providing a kind of DSL that allow you to create json files leveraging the dynamism of anonymous records since they behave almost like javascript objects.

Writing commands should be almost painless these commands produce a JSON string that can be utilized directly on your application or even copy/pasted into the mongo shell

Commands are kind of a version of raw sql queries but they allow you to do what you already know how to do without much changes to the objects you might be manipulating already

Sample Usage

Check out this quick sample of the work so far

open System
open MongoDB.Driver
open MongoDB.Bson
open Mondocks.Queries
open Mondocks.Types

let createUsers minAge maxAge = 
    let random  = Random()
    insert "users" {
        documents 
            [
                {| name = "Peter"; age = random.Next(minAge, maxAge); |}
                {| name = "Sandra"; age = random.Next(minAge, maxAge); |}
                {| name = "Mike"; age = random.Next(minAge, maxAge); |}
                {| name = "Perla"; age = random.Next(minAge, maxAge); |}
                {| name = "Updateme"; age = 1; |}
                {| name = "Deleteme"; age = 50; |}
            ]
    }

let updateUser (name: string) (newName: string) =
    update "users" {
        updates
            [
                { q = {| name = name |} 
                  u = {| name = newName; age = 5 |}
                  multi = Some false
                  upsert = Some false
                  collation = None 
                  arrayFilters = None
                  hint = None }
            ]
    }


let deleteUser (name: string) = 
    delete "users" {
        deletes [
            { q = {| name = name |}
              limit = 1
              collation = None
              hint = None
              comment = None }
        ]
    }

// Define a function to construct a message to print
let getUsersOverAge (age: int) =
    find "users" {
        filter {| age = {| ``$gt``= age |} |}
        limit 2
        skip 1
    }
type User = { _id: ObjectId; name: string; age: int }
[<EntryPoint>]
let main argv =
    let client = MongoClient("mongodb://localhost:27017")
    let db = client.GetDatabase("mondocks")

    let userscmd = createUsers 15 50
    let result = db.RunCommand<InsertResult>(JsonCommand userscmd)
    printfn $"InsertResult: %A{result}"

    let over20 = getUsersOverAge 20
    let result = db.RunCommand<FindResult<User>>(JsonCommand over20)
    printfn $"FindResult Ok: %d{result.ok}"
    result.cursor.firstBatch |> Seq.iter (fun value -> printfn $"%A{value}")

    let updatecmd = updateUser "Updateme" "Updated"
    let result = db.RunCommand<UpdateResult>(JsonCommand updatecmd)
    printfn $"UpdateResult: %A{result}"

    let deletecmd = deleteUser "Deleteme"
    let result = db.RunCommand<DeleteResult>(JsonCommand deletecmd)
    printfn $"DeleteResult: %A{result}"
    
    0 // return an integer exit code// return an integer exit code

Thanks for the early feedback in twitter from Isaac, Zaid, Alexey, Alexander, and the F# community
you can follow it on the first issue

WIP

This is a work in progress, you can help providing feedback about it's usage

Samples

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.3.18 57 12/23/2020
0.3.15 56 12/17/2020
0.2.0 81 12/7/2020
0.1.0 108 11/27/2020
0.1.0-beta3 76 11/23/2020
0.1.0-beta2 128 11/23/2020
0.1.0-beta1 58 11/22/2020
Show less