promise-mongo
a mongodb wrapper, make everything return promise.
intro
with promise-mongo
we can do it like this:
dbuser //ordbuser //or in koa/covar user = dbuser
install note
if failed when installing kerberos, you may need sudo apt-get install libkrb5-dev
about promise
if no global.Promise
, will use promise
module.
migrate from 0.3 to 0.4
0.3 support mongodb 1.4.3, 0.4 support mongodb 2.0.25, but old methods are all implanted, nothing would break. a few tips:
- add new methods:
deleteOne
,deleteMany
,updateOne
,updateMany
,insertOne
,insertMany
,findOneAndUpdate
andfindOneAndDelete
- removed
drop
since it will break the collection remove
,update
,insert
methods are marked for deprecation and will be removed in a future 3.0 driver, butpromise-mongo
still support them, inside the functionpromise-mongo
usedeleteOne
,deleteMany
,updateOne
,updateMany
,insertOne
,insertMany
, so no worries.- but use
deleteOne
,deleteMany
,updateOne
,updateMany
,insertOne
,insertMany
is still preferred for performance reason. collection.find
in 2.0.25 prefer chain command likecollection.find().limit(1)
instead ofcollection.find({}, { limit: 1})
, but it is still supported for now.findAndModify
is deprecated in 2.0.25, butpromise-mongo
still support it by proxy it tofindOneAndUpdate
andfindOneAndDelete
, butfindOneAndUpdate
andfindOneAndDelete
is preferred for performance reason.- use
new Server('100.100.5.100', 27017)
instead ofnew Server( { host: '100.100.5.100', port: '27017' } )
Installation
$ npm install promise-mongo
use
see more example from test/test.js
var PM = var pm = var collectionNames = 'user' 'book' 'post' pm
use replset
var PM = var pm = var collectionNames = 'user' 'book' 'post' var mongo = PMmongoRepelSet = mongoReplSetServer = mongoServerrepels = '100.100.5.100' 27017 '100.100.5.99' 27017 '100.100.5.98' 27017 pm
docs
promise-mongo
did not cover all the collection and cursor methods yet.
collection methods:
dbcollectionNamedb.collectionName.save = function(doc) db.collectionName.find = function(query, options) db.collectionName.update = function(selector, doc, options) //deprecateddb.collectionName.updateOne = function(selector, doc, options) db.collectionName.updateMany = function(selector, doc, options) db.collectionName.remove = function(selector, options) //deprecateddb.collectionName.deleteOne = function(selector, options) db.collectionName.deleteMany = function(selector, options) db.collectionName.group = function(keys, condition, initial, reduce, finalize, command, options) db.collectionName.insert = function(doc, options) //deprecateddb.collectionName.insertMany = function(docs, options) db.collectionName.insertOne = function(doc, options) db.collectionName.mapReduce = function(map, reduce, options) db.collectionName.count = function(query, options) db.collectionName.drop = function() db.collectionName.findAndModify = function(query, sort, doc, options) //deprecateddb.collectionName.findOneAndUpdate = function(filter, update, options) db.collectionName.findOneAndDelete = function(filter, options)
read http://mongodb.github.io/node-mongodb-native/2.0/api-docs/ for details.
cursor methods:
cf//Deprecatedcf.toArray = function(cursor) //Deprecatedcf.limit = function(cursor, count) //Deprecatedcf.skip = function(cursor, count) //Deprecated
since 1.1.0 use cur.cursorMethod
instead of cf.cursorMethod
, but cf.cursorMethod
is still keeped for legacy reason, so nothing would break.
curcur.toArray = function() cur.limit = function(count) cur.skip = function(count)
exposed function & reference
//make a callback function return promisepmtoPromise //pm.mongo = PM.mongo = require('mongodb')pmmongo //or PM.mongo //mongodb instancepmmdb
test
make sure 'mongodb://127.0.0.1:27017/test'
is available or edit test yourself.
git clone https://github.com/zxdong262/promise-mongo.gitcd promise-mongosudo npm installsudo npm install mocha -gmocha
change log
3.0.0 rewrite with es6 feature, drop promise/lodash module dependency, support node 4.0+ only, use mongodb native 2.1.7.
2.0.0 use mongodb 2.0.44, compatible with node v4, you may need install libkrb5-dev
by sudo apt-get install libkrb5-dev
or something else.
1.1.0 add pm.cur.cursorMethods
0.4.2 add test for mapReduce
0.4.1 support mongodb 2.0.27, all methods have test, group
have default options
0.4.0 support mongodb 2.0.25, rewrite tests.
0.3.0
- collection.find() now have
options
param. - add tests for collection.find(), collection.remove(), collection.count()
0.2.1 use mongodb 1.4 (not compatible with 2.0.25 yet).
0.2.0 include promise module for non-es6 user.
0.1.4 code optimazation, use inline slice(arguments)
0.1.3 code optimazation, do not use argument directly(but still use slice)
0.1.2 expose mongodb instance to pm.mdb
0.1.1 expose PM.mongo
0.1.0 now return PM instance, support multi connection
0.0.3 expose mongodb to pm.mongo
0.0.2 add cursor.skip
license
MIT