On this page:
4.1 Dictionaries
create-mongo-dict
mongo-dict-query
mongo-dict?
current-mongo-db
mongo-dict-ref
mongo-dict-set!
mongo-dict-remove!
mongo-dict-count
mongo-dict-inc!
mongo-dict-push!
mongo-dict-append!
mongo-dict-set-add!
mongo-dict-set-add*!
mongo-dict-pop!
mongo-dict-shift!
mongo-dict-pull!
mongo-dict-pull*!
4.2 Structures
define-mongo-struct

4 ORM Operations

 (require (planet jaymccarthy/mongodb:1:0/orm/main))

An "ORM" style API is built on the basic Mongo operations.

4.1 Dictionaries

 (require (planet jaymccarthy/mongodb:1:0/orm/dict))

A Mongo dictionary is a dictionary backed by Mongo.

(create-mongo-dict col)  mongo-dict?
  col : string?
Creates a new Mongo dictionary in the col collection of the (current-mongo-db) database.

(mongo-dict-query col query)  (sequenceof mongo-dict?)
  col : string?
  query : bson-document/c
Queries the collection and returns Mongo dictionaries.

(mongo-dict? x)  boolean?
  x : any/c
A test for Mongo dictionaries.
(current-mongo-db)  (or/c false/c mongo-db?)
(current-mongo-db db)  void?
  db : (or/c false/c mongo-db?)
The database used in Mongo dictionary operations.
(mongo-dict-ref md key [fail])  any/c
  md : mongo-dict?
  key : symbol?
  fail : any/c = bson-null
Like dict-ref but for Mongo dictionaries, returns bson-null by default on errors or missing values.
(mongo-dict-set! md key val)  void
  md : mongo-dict?
  key : symbol?
  val : any/c
(mongo-dict-remove! md key)  void
  md : mongo-dict?
  key : symbol?
(mongo-dict-count md)  exact-nonnegative-integer?
  md : mongo-dict?

(mongo-dict-inc! md key [amt])  void
  md : mongo-dict?
  key : symbol?
  amt : number? = 1
Increments key’s value by amt atomically.
(mongo-dict-push! md key val)  void
  md : mongo-dict?
  key : symbol?
  val : any/c
Pushes a value onto the sequence atomically.
(mongo-dict-append! md key vals)  void
  md : mongo-dict?
  key : symbol?
  vals : sequence?
Pushes a sequence of values onto the sequence atomically.
(mongo-dict-set-add! md key val)  void
  md : mongo-dict?
  key : symbol?
  val : any/c
Adds a value to the sequence if it is not present atomically.
(mongo-dict-set-add*! md key vals)  void
  md : mongo-dict?
  key : symbol?
  vals : sequence?
Adds a sequence of values to the sequence if they are not present atomically.
(mongo-dict-pop! md key)  void
  md : mongo-dict?
  key : symbol?
Pops a value off the sequence atomically.
(mongo-dict-shift! md key)  void
  md : mongo-dict?
  key : symbol?
Shifts a value off the sequence atomically.
(mongo-dict-pull! md key val)  void
  md : mongo-dict?
  key : symbol?
  val : any/c
Remove a value to the sequence if it is present atomically.
(mongo-dict-pull*! md key vals)  void
  md : mongo-dict?
  key : symbol?
  vals : sequence?
Removes a sequence of values to the sequence if they are present atomically.

4.2 Structures

 (require (planet jaymccarthy/mongodb:1:0/orm/struct))

define-mongo-struct is a macro to create some convenience functions for Mongo dictionaries.

(define-mongo-struct struct collection
  ([field opt ...]
   ...))
 
opt = #:required
  | #:immutable
  | #:ref
  | #:set!
  | #:inc
  | #:null
  | #:push
  | #:append
  | #:set-add
  | #:set-add*
  | #:pop
  | #:shift
  | #:pull
  | #:pull*
 
  struct : identifier?
  collection : string?
  field : identifier?
Defines make-struct and a set of operations for the fields.

Every field implicitly has the #:ref option. Every mutable field implicitly has the #:set! option. Every immutable field implicitly has the #:required option. It is an error for an immutable field to have any options other than #:required and #:ref, which are both implicit.

make-struct takes one keyword argument per field. If the field does not have the #:required option, the argument is optional and the instance will not contain a value for the field. make-struct returns a mongo-dict?.

If a field has the #:ref option, then struct-field is defined. It is implemented with mongo-dict-ref.

If a field has the #:set option, then set-struct-field! is defined. It is implemented with mongo-dict-set!.

If a field has the #:inc option, then inc-struct-field! is defined. It is implemented with mongo-dict-inc!.

If a field has the #:null option, then null-struct-field! is defined. It is implemented with mongo-dict-remove!.

If a field has the #:push option, then push-struct-field! is defined. It is implemented with mongo-dict-push!.

If a field has the #:append option, then append-struct-field! is defined. It is implemented with mongo-dict-append!.

If a field has the #:set-add option, then set-add-struct-field! is defined. It is implemented with mongo-dict-set-add!.

If a field has the #:set-add* option, then set-add*-struct-field! is defined. It is implemented with mongo-dict-set-add*!.

If a field has the #:pop option, then pop-struct-field! is defined. It is implemented with mongo-dict-pop!.

If a field has the #:shift option, then shift-struct-field! is defined. It is implemented with mongo-dict-shift!.

If a field has the #:pull option, then pull-struct-field! is defined. It is implemented with mongo-dict-pull!.

If a field has the #:pull* option, then pull*-struct-field! is defined. It is implemented with mongo-dict-pull*!.