Are the answers in this question still relevant: How can I promisify the MongoDB native Javascript driver using bluebird?
I don't know since when this was updated, but 2.0 JS driver for MongoDB has a property in the options object promiseLibrary
: http://mongodb.github.io/node-mongodb-native/2.0/api/MongoClient.html
And most methods/functions do return a promise, like for instance Cursor.toArray()
.
However, I can't find an example using this new option, but wouldn't it be simpler just using:
MongoClient.connect('mongodb://URL', { promiseLibrary: require('bluebird') });
Or is this definition wrong? - In which case, how should it be properly defined?
Update:
The code is running on io.js so I might not even need to specify a promiseLibrary
as the driver would use ES6 promises? - However, supposedly bluebird promises are slower:
Why are native ES6 promises slower and more memory-intensive than bluebird?
Update2: I've added the bluebird tag - maybe the guys working on bluebird can provide more details if promisifying is really better than using MongoDB's own implementation?
Is it still necessary to promisify the MongoDB driver?
No, they now have built in support for promise libraries like bluebird. I think we should ask the question differently though:
Is it a good idea to promisify the MongoDB driver?
Perhaps, bluebird does a better job at converting callback APIs to promises than the Mongo driver does internally by wrapping much more lightly. It will likely be faster to still promisify as "in the before days".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With