magnolia–A beautiful MongoDB driver w/ Q
Magnolia implements a coherent, lazy, & chainable interface... with promises! Don't nest callbacks anymore than you have to!
Init
If you don't mind state, you can init the module's state with default options.
var mongo = ObjectID = mongoObjectID; mongo
Or you can start a chain with the defaults you would like.
var mongo =
Connection
magnolia(collection, [db])
.collection(collection)
.db(db)
.server(server)
.options(...)
m:1
journal:true
fsync:true
slaveOk:true
Find
// filter! // just find one! ; ;
.toArray([cb])
Query the collection, otherwise it will be lazily queried when you evaluate the chain as a promise.filter(criteria)
Filter the collection.one()
Find one! And return the document, instead of a list..limit(n).skip(m)
to control paging..sort(fields)
Order by the given fields. There are several equivalent syntaxes:.sort([['field1', 'desc'], ['field2', 'asc']])
.sort([['field1', 'desc'], 'field2'])
.sort('field1')
ascending by field1
Find and modifiy
optionsoptions ;
Useful options (including the previous options):
.filter(...)
.sort(...)
.options(...)
remove:true
set to a true to remove the object before returningnew:true
set to true if you want to return the modified object rather than the original. Ignored for remove.upsert:true
Atomically inserts the document if no documents matched.
Remove
;
Insert
; ;
.safe()
or.unsafe()
Make sure document is in the database before returning.options(...)
safe:true
Update; update and insert (upsert)
Signature:
;
;
Useful options:
.filter(...)
.one()
or.multi()
.safe()
or.unsafe()
.options(...)
safe:true
Should always set if you have a callback.multi:true
If set, all matching documents are updated, not just the first.upsert:true
Atomically inserts the document if no documents matched.
Save
Performs an update if there's an _id
, and an insert if not!
; // update; // insert
Count
;
Map/reduce
Data types
magnoliamagnoliamagnoliamagnoliamagnoliamagnoliamagnoliamagnoliamagnoliamagnolia