The library wraps the 2.0.x driver with promises allowing you to access most of the functionality of the 2.0.x driver using your favorite Promise library.
var co = require('co')
, MongoClient = require('mongodb-promisified')().MongoClient;
co(function*() {
// Connect to the database
var client = yield MongoClient.connect('mongodb://localhost:27017/db');
// Insert a document
var result = yield client.collection('test1').insertOne({a:1});
// Get the documents
var docs = yield client.collection('test1').find({}).toArray();
// Close the connection
yield client.close();
}).catch(function(err) {
console.log(err.stack);
});
Pretty straightforward to use the library with the generator library co
and ES6. To use your favorite Promise library simply do the following when requiring the module.
var co = require('co')
, Promise = require('bluebird')
, MongoClient = require('mongodb-promisified')(Promise).MongoClient;
Grid FS is not supported by this driver.
The promisified wrapper supports most of the functionality of the 2.0 drivers but not all of it.
The supported functions are.
close
command
createCollection
stats
eval
renameCollection
dropCollection
dropDatabase
collections
executeDbAdminCommand
addUser
removeUser
authenticate
logout
collection
listCollections
db
insertOne
insertMany
bulkWrite
updateOne
replaceOne
updateMany
deleteOne
deleteMany
save
findOne
rename
options
createIndex
createIndexes
isCapped
dropIndex
dropIndexes
reIndex
ensureIndex
indexExists
indexInformation
indexes
count
distinct
stats
findOneAndDelete
findOneAndReplace
findOneAndUpdate
parallelCollectionScan
geoNear
geoHaystackSearch
group
mapReduce
drop
find
aggregate
listIndexes
toArray
next
filter
addCursorFlag
addQueryModifier
comment
maxTimeMS
maxTimeMs
project
sort
batchSize
limit
skip
toArray
next
batchSize
geoNear
group
limit
match
maxTimeMS
out
project
redact
skip
sort
unwind