Skip to content

Instantly share code, notes, and snippets.

@zhangchiqing
Last active November 9, 2015 20:08
Show Gist options
  • Save zhangchiqing/2edf9e62493356335f53 to your computer and use it in GitHub Desktop.
Save zhangchiqing/2edf9e62493356335f53 to your computer and use it in GitHub Desktop.
Iterate over all mongo documents in a collection
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