Monk is a tiny layer that provides some usability improvements for MongoDB usage within Node.JS
Features (taken from monks github page):
- Command buffering. You can start querying right away.
- Promises built-in for all queries. Easy interoperability with modules.
- Easy connections / configuration
- Well-designed signatures
- Improvements to the MongoDB APIs (eg:
findAndModify
supports theupdate
signature style)- Auto-casting of
_id
in queries- Builds on top of mongoskin
- Allows to set global options or collection-level options for queries. (eg:
safe
istrue
by default for all queries)
Connection
var db = require('monk')('localhost/dbname');
To set global multi-doc at db level
db.options.multi = true;
To set global multi-doc at collection level
db.get('users').options.multi = false;
Close DB connection
db.close();
Operations
var collection = db.get('collection');
collection.insert({ }, function(err) { });
collection.find({ }, function(err, docs) { });
collection.findOne({ }, function(err, doc) { });
collection.update({ }, { }, function(err, docs) { });
collection.findAndModify({ }, { }, function(err, docs) { });
collection.findById('id', function(err, doc) { });
collection.drop(function(err) { });
Index
collection.index('name.first', function() { });
Unique Index
collection.index('email', { unique: true });
Compound Index
collection.index('name.first name.last')
Compound with sort
collection.index({ 'email': 1, 'password': -1 });
To get indexes for a collection
collection.indexes(fn);
Drop an index
collection.dropIndex(name, fn);
Drop all indexes
collection.dropIndexes(fn);