Last active
July 27, 2021 22:39
-
-
Save AngelMunoz/35cf2bc439da9969664f9987f7109ee3 to your computer and use it in GitHub Desktop.
These are Mondocks Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: Mondocks.Net" | |
open System | |
open Mondocks.Queries | |
type UserParams = | |
{ name: string; age: int; } | |
let insertModel<'T> (collection: string) (values: seq<'T>) = | |
insert collection { | |
documents values | |
} | |
let insertUsers = | |
let users = | |
[ for i in 0..10 do | |
{ name = $"User %i{i}"; age = (i + 1) * 10 }] | |
insertModel "users" users | |
let insertBooks = | |
let books = | |
[ for i in 0..10 do | |
// fake isbn | |
{| _id = $"isbn:%i{i}"; author = $"Author: %i{i}"; published = {| ``$date`` = DateTimeOffset.Now.ToString("o") |} |}] | |
insertModel "books" books | |
printfn $"insertUsers: {insertUsers}\ninsertBooks: {insertBooks}"; | |
(* insertUsers: | |
{ | |
"insert":"users", | |
"documents":[ | |
{ | |
"name":"User 0", | |
"age":10 | |
}, | |
{ | |
"name":"User 1", | |
"age":20 | |
}, | |
{ | |
"name":"User 2", | |
"age":30 | |
}, | |
{ | |
"name":"User 3", | |
"age":40 | |
}, | |
{ | |
"name":"User 4", | |
"age":50 | |
}, | |
{ | |
"name":"User 5", | |
"age":60 | |
}, | |
{ | |
"name":"User 6", | |
"age":70 | |
}, | |
{ | |
"name":"User 7", | |
"age":80 | |
}, | |
{ | |
"name":"User 8", | |
"age":90 | |
}, | |
{ | |
"name":"User 9", | |
"age":100 | |
}, | |
{ | |
"name":"User 10", | |
"age":110 | |
} | |
] | |
} | |
*) | |
(* insertBooks: | |
{ | |
"insert":"books", | |
"documents":[ | |
{ | |
"_id":"isbn:0", | |
"author":"Author: 0", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8897971-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:1", | |
"author":"Author: 1", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:2", | |
"author":"Author: 2", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:3", | |
"author":"Author: 3", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:4", | |
"author":"Author: 4", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:5", | |
"author":"Author: 5", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:6", | |
"author":"Author: 6", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:7", | |
"author":"Author: 7", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:8", | |
"author":"Author: 8", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:9", | |
"author":"Author: 9", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
}, | |
{ | |
"_id":"isbn:10", | |
"author":"Author: 10", | |
"published":{ | |
"$date":"2020-11-29T21:21:15.8918304-07:00" | |
} | |
} | |
] | |
} | |
*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: Mondocks.Net" | |
open Mondocks.Types | |
open Mondocks.Queries | |
let deleteAllPerQuery (collection: string) (deletes: seq<'Query>) = | |
let deleteDefs = | |
deletes | |
|> Seq.map(fun del -> { q = del; limit = 0; collation = None; hint = None; comment = None}) | |
delete collection { | |
deletes deleteDefs | |
} | |
let deleteLimitedPerQuery (collection: string) (deletes: seq<{| query: 'Query; limit: int |}>) = | |
let deleteDefs = | |
deletes | |
|> Seq.map(fun def -> { q = def.query; limit = def.limit; collation = None; hint = None; comment = None}) | |
delete collection { | |
deletes deleteDefs | |
} | |
let deleteUsersWhereEmailInBlockList = | |
let blocklistemails = ["im@scammer.com"; "im@forbidden.com"] | |
deleteAllPerQuery "users" [{| email = {| ``$in`` = blocklistemails |} |}] | |
let deletePlaces = | |
let deletes = [ | |
{| query = box | |
{| name = | |
{| ``$regularExpression``= | |
{| pattern = "^([a-z0-9]){0,}-x{3}$"; options = "i" |} |} |} | |
limit = 10 |} | |
{| query = box {| name = "deleteme" |} |> box; limit = 2 |} | |
{| query = box {| _id = {| ``$oid`` = "fbcb73a5f3bc" |} |}; limit = 5 |} | |
] | |
deleteLimitedPerQuery "places" deletes | |
printfn $"deleteUsersWhereEmailInBlockList: {deleteUsersWhereEmailInBlockList}\ndeletePlaces: {deletePlaces}" | |
(* deleteUsersWhereEmailInBlockList: | |
{ | |
"delete":"users", | |
"deletes":[ | |
{ | |
"q":{ | |
"email":{ | |
"$in":[ | |
"im@scammer.com", | |
"im@forbidden.com" | |
] | |
} | |
}, | |
"limit":0 | |
} | |
] | |
} | |
*) | |
(* deletePlaces: | |
{ | |
"delete":"places", | |
"deletes":[ | |
{ | |
"q":{ | |
"name":{ | |
"$regularExpression":{ | |
"options":"i", | |
"pattern":"^([a-z0-9]){0,}-x{3}$" | |
} | |
} | |
}, | |
"limit":10 | |
}, | |
{ | |
"q":{ | |
"name":"deleteme" | |
}, | |
"limit":2 | |
}, | |
{ | |
"q":{ | |
"_id":{ | |
"$oid":"fbcb73a5f3bc" | |
} | |
}, | |
"limit":5 | |
} | |
] | |
} | |
*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: Mondocks.Net" | |
open Mondocks.Queries | |
let filterByName (name: string) = | |
{| name = name |} | |
let filterbyNameAndId (name: string) (id: string) = | |
let nameFilter = filterByName name | |
{| nameFilter with owner = {| ``$oid`` = id |} |} | |
let getUsersByName (name: string) (pagination: int * int) = | |
let offset, take = pagination | |
find "places" { | |
filter (filterByName name) | |
skip offset | |
limit take | |
} | |
let getPlacesByNameAndOwner (name: string) (owner: string) (pagination: int * int) = | |
let offset, take = pagination | |
find "users" { | |
filter (filterbyNameAndId name owner) | |
skip offset | |
limit take | |
} | |
let getMikes = getUsersByName "Mike" (0, 10) | |
let getPlacesFromMike = | |
// I'll use a fake ObjectId just to show | |
getPlacesByNameAndOwner "Travel Points" "5fc32b8456e87e4f021f43e2" (0, 10) | |
printfn $"getMikes Command: %s{getMikes}\ngetPlacesFromMike: %s{getPlacesFromMike}" | |
/// prints to the console | |
/// getMikes Command: {"find":"places","filter":{"name":"Mike"},"skip":0,"limit":10} | |
/// getPlacesFromMike: {"find":"users","filter":{"name":"Travel Points","owner":{"$oid":"5fc32b8456e87e4f021f43e2"}},"skip":0,"limit":10} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: Mondocks.Net" | |
open System | |
open Mondocks.Types | |
open Mondocks.Queries | |
type Book = | |
{ _id: string; | |
author: string; | |
// other potential fields | |
} | |
let filterbyISBN (isbn: string) = | |
{| _id = isbn |} | |
let updateBook (isbn: string) (updateFields: Book) = | |
// updateFields can be a record like this { _id = "977877"; author = "Updated Author" } | |
update "books" { | |
updates [ { q = filterbyISBN isbn | |
u = updateFields | |
multi = Some false | |
upsert = Some false | |
collation = None | |
arrayFilters = None | |
hint = None } ] | |
} | |
let multipleUpdates(updates: seq<'TFilter * 'TUpdate>) = | |
let updateDefs = | |
updates | |
|> Seq.map(fun (filter, update) -> | |
{ q = filter | |
u = update | |
multi = Some false | |
upsert = Some true | |
collation = None | |
arrayFilters = None | |
hint = None }) | |
update "places" { | |
updates updateDefs | |
} | |
let updateFranksBookCmd = | |
// suppose the author was previously just Frank, and now it will be Frank .D | |
updateBook "1234567890" { _id = "1234567890"; author = "Frank D." } | |
let updateTravelPointsCmd = | |
let updates = | |
[({| name = "Travel Point a" |}, | |
{| name = "Travel Point A"; | |
updatedAt = {|``$date`` = DateTime.Now.ToString("o") |} |} |> box) | |
({| name = "BFF's House" |}, | |
// you can do updates with different data types, but you'll need to box them | |
{| name = "Not BFF's House"; | |
notes = "I'll never talk to him again" | |
updatedAt = {|``$date`` = DateTime.Now.ToString("o") |} |} |> box) | |
({| name = "Convention Centre" |}, | |
{| name = "Old Convention Centre"; | |
address = "Old Street 123, Somewhere" | |
updatedAt = {|``$date`` = DateTime.Now.ToString("o") |} |} |> box) | |
] | |
multipleUpdates updates | |
printfn $"updateFranksBookCmd: {updateFranksBookCmd}\nupdateTravelPointsCmd: {updateTravelPointsCmd}" | |
// prints in the console | |
// updateFranksBookCmd: {"update":"books","updates":[{"q":{"_id":"1234567890"},"u":{"_id":"1234567890","author":"Frank D."},"upsert":false,"multi":false}]} | |
(* | |
I had to format this one | |
updateTravelPointsCmd: { | |
"update": "places", | |
"updates": [ | |
{ | |
"q": { | |
"name": "Travel Point a" | |
}, | |
"u": { | |
"name": "Travel Point A", | |
"updatedAt": { | |
"$date": "2020-11-28T23:05:40.0396288-07:00" | |
} | |
}, | |
"upsert": true, | |
"multi": false | |
}, | |
{ | |
"q": { | |
"name": "BFF's House" | |
}, | |
"u": { | |
"name": "Not BFF's House", | |
"notes": "I'll never talk to him again", | |
"updatedAt": { | |
"$date": "2020-11-28T23:05:40.0396288-07:00" | |
} | |
}, | |
"upsert": true, | |
"multi": false | |
}, | |
{ | |
"q": { | |
"name": "Convention Centre" | |
}, | |
"u": { | |
"address": "Old Street 123, Somewhere", | |
"name": "Old Convention Centre", | |
"updatedAt": { | |
"$date": "2020-11-28T23:05:40.0396288-07:00" | |
} | |
}, | |
"upsert": true, | |
"multi": false | |
} | |
] | |
} | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment