pmongo
Promised wrapper for mongodb native driver
Install
Pmongo is available through npm:
npm install pmongo
Pmongo is based on native Promises.
That's why you must have nodejs 0.11 version or greater or iojs
Also pmongo depends on node-mongodb-native but it is not included
in package.json
you must specify it as a dependency in your root project
Usage
Use pmongo just like mongojs, except you can also use the returned promise instead of the callback. Note that a promise isn't returned if a callback is specified.
var pmongo = ;var db = ;
The connection string should follow the format desribed in the mongo connection string docs. Some examples of this could be:
// simple usage for a local dbvar db = ;// the db is on a remote server (the port default to mongo)var db = ;// we can also provide some credentialsvar db = ;// connect now, and worry about collections latervar db = ;var mycollection = db;
After we connected we can query or update the database just how we would using the mongo API with the exception that the functions return
a promise for the result rather than the result itself. Cursor operations such as find()
and sort()
return a cursor; to get a
promise for the result, you have to force evaluation using toArray()
. Alternatively, you can just call then()
on the cursor and it will call toArray()
for you, returning a promise. The function findOne()
returns a promise immediately, not a cursor.
// find everythingdbmycollection;// find everything, but sort by namedbmycollection;// find a document using a native ObjectIddbmycollection;// find all named 'mathias' and increment their leveldbmycollection;// find one named 'mathias', tag him as a contributor and return the modified docdbmycollection;// use the save function to just save a documentdbmycollection;
The forEach
function is a special case. The library supports the mongojs style:
// iterate over all whose level is greater than 90.dbmycollection;
It also supports a promise version. If you pass a callback to the forEach
function with only one argument, you get the promise version. The promise will resolve (with undefined
) when the callback has been called for all documents.
// iterate over all whose level is greater than 90 (promise version)dbmycollection;
If you provide a callback to find
or any cursor config operation mongojs will call toArray
for you
dbmycollection;dbmycollection;
is the same as
dbmycollection;dbmycollection;
If you are using the promises API, you must call toArray() on cursors before a promise can be obtained. E.g.:
dbmycollection;
For more detailed information about the different usages of update and querying see the mongo docs
Streaming cursors
All cursors are a readable stream of objects.
var JSONStream = ;// pipe all documents in mycollection to stdoutdbmycollection;
Notice that you should pipe the cursor through a stringifier (like JSONStream) if you want to pipe it to a serial stream like a http response.
Tailable cursors
If you are using a capped collection you can create a tailable cursor to that collection by adding tailable:true
to the find options
var cursor = dbmycollection;// since all cursors are streams we can just listen for datacursor;
Note that you need to explicitly set the selection parameter in the find
call.
Database commands
With pmongo you can run database commands just like with the mongo shell using db.runCommand()
db;
or db.collection.runCommand()
dbthings;
Replication Sets
Pmongo can also connect to a mongo replication set by providing a connection string with multiple hosts
var db = ;
For more detailed information about replica sets see the mongo replication docs