Install

Compatability with previous versions

I no longer use Q for promises. This means that you can't use
.done() at the end of promise chains. To turn on compatability with previous versions, you can
call the compatible() function:

var pmongo =require('promised-mongo').compatible();

Other than dropping support for callbacks, I have tried to make sure that the new library is
compatible with the old tests (see the legacy_tests folder).

How I write JavaScript

I like using async functions from current
EMCAScript proposals. This makes node amazingly easier to understand. An example from the tests:

it('returns all documents',asyncfunction(){

let docs =[{hello:'world'},{hello:'kitty'}];

awaitcollection.insert(docs);

let cursor =collection.find();

expect(cursor).to.be.an.instanceof(Cursor);

let result =awaitcursor.toArray();

expect(result).to.deep.have.members(docs);

});

Isn't it so much easier to understand? The downsides (of course there had to be some) is that since
this is so bleeding edge, editor support and debugging support are varied and patchy, and there's a
chance that the feature won't even make it to the final language specification.

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.
Note that due to limitations in the Q promise library, you should call .done() at the end of
any promise chain you aren't returning, in order to throw any uncaught exceptions. For brevity, the examples in this readme don't do that.

// find one named 'mathias', tag him as a contributor and return the modified doc

db.mycollection.findAndModify({

query:{ name:'mathias'},

update:{ $set:{ tag:'maintainer'}},

new:true

})

.then(function(doc){

// doc.tag === 'maintainer'

});

// use the save function to just save a document

db.mycollection.save({created:'just now'});

The forEach function is a special case. The library supports the mongojs style:

// iterate over all whose level is greater than 90.

db.mycollection.find({level:{$gt:90}}).forEach(function(err,doc){

if(doc){

//do things with doc

}else{

//the callback gets called at the end with a null doc

console.log('Finished!');

}

});

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)

db.mycollection.find({level:{$gt:90}}).forEach(function(doc){

//do things with doc

})

.then(function(){

console.log('Finished!');

});

To access lastErrorObject returned by findAndModify using the promises API, use the findAndModifyEx function:

db.mycollection.findAndModifyEx({

query: { name: 'mathias' },

update: { $set: { tag:'maintainer' } },

new: true

})

.then(function(result) {

var doc = result.result;

var lastErrorObject = result.lastErrorObject;

});

If you provide a callback to find or any cursor config operation mongojs will call toArray for you