Last active
November 9, 2015 20:08
-
-
Save zhangchiqing/2edf9e62493356335f53 to your computer and use it in GitHub Desktop.
Iterate over all mongo documents in a collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongo = require('mongodb'); | |
var config = { | |
host: 'localhost', | |
port: '27017', | |
db: 'local', | |
}; | |
var getDBAsync = function(config) { | |
return new Promise(function(resolve, reject) { | |
var server = new mongodb.Server(config.host, config.port, {}); | |
new mongodb.Db(config.db, server, { safe: true }).open(function(err, db) { | |
if (err) { return reject(err); } | |
resolve(db); | |
}); | |
}); | |
}; | |
getDBAsync(config).then(function(db) { | |
var cursor = db.find({}); | |
// new inserted documents after the above find won't be returned by `nextCursor`; | |
cursor.nextCursor(function(err, doc) { | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment