monk
Monk is a tiny layer that provides simple yet substantial usability improvements for MongoDB usage within Node.JS.
var db = 'localhost/mydb' users = db usersindex'name last';users;users; db
Features
- 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)
How to use
Connecting
Single server
var db = 'localhost/mydb'
Replica set
var db = 'localhost/mydb,192.168.1.1'
Disconnecting
db
Collections
Getting one
var users = db// users.insert(), users.update() … (see below)
Dropping
users;
Signatures
- All commands accept the simple
data[, …], fn
. For examplefind({}, fn)
findOne({}, fn)
update({}, {}, fn)
findAndModify({}, {}, fn)
findById('id', fn)
- You can pass options in the middle:
data[, …], options, fn
- You can pass fields to select as an array:
data[, …], ['field', …], fn
- You can pass fields as a string delimited by spaces:
data[, …], 'field1 field2', fn
- To exclude a field, prefix the field name with '-':
data[, …], '-field1', fn
Promises
All methods that perform an async action return a promise.
var promise = users;promisetype; // 'insert' in this casepromise;promise;promise;promise;promise;
Indexes
usersindex'name.first' fn;usersindex'email' unique: true ; // uniqueusersindex'name.first name.last' // compoundusersindex 'email': 1 'password': -1 ; // compound with sortusersindex'email' sparse: true fn; // with optionsusers; // get indexesusers; // drop an indexusers; // drop all indexes
Inserting
users;
Casting
To cast to ObjectId
:
usersid // returns new generated ObjectIDusersid'hexstring' // returns ObjectIdusersidobj // returns ObjectId
Updating
users;users;
Finding
Many
users;
By ID
users;users;
Single doc
findOne
also provides the findById
functionality.
users;
And modify
users;users;
Streaming
Note: stream: true
is optional if you register an each
handler in the
same tick. In the following example I just include it for extra clarity.
users ;
Destroying a cursor
On the returned promise you can call destroy()
. Upon the cursor
closing the success
event will be emitted.
Global options
var db = 'localhost/mydb'dboptionsmulti = true; // global multi-doc updatedboptionsmulti = false; // collection-level
Monk sets safe
to true
by default.
Query debugging
If you wish to see what queries monk
passes to the driver, simply leverage
debug:
DEBUG="monk:queries"
To see all debugging output:
DEBUG="monk:*"
License
(The MIT License)
Copyright (c) 2012 Guillermo Rauch <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.