Last active
June 12, 2016 15:59
-
-
Save pulkitsinghal/961663ab00355199124cca12c41def28 to your computer and use it in GitHub Desktop.
nodejs mongo workers
This file contains hidden or 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 dbUrl = 'mongodb://username:[email protected]:port/database-name'; | |
MongoClient.connect( | |
dbUrl, | |
function(err, db) { | |
if(err) throw err; | |
db.collection('ItemModel').find({}).toArray(function(err, docs) { | |
console.dir(docs); | |
console.dir(docs.length); | |
db.close(); | |
}); | |
}); |
This file contains hidden or 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 dbUrl = 'mongodb://username:[email protected]:port/database-name'; | |
MongoClient.connect( | |
dbUrl, | |
function(err, db) { | |
if(err) throw err; | |
db.collection('ItemModel').findOne({}, function(err, doc) { | |
console.dir(doc); | |
db.close(); | |
}); | |
}); |
This file contains hidden or 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
// Inject bluebird as mongo preferred promise library: | |
// https://easyusedev.wordpress.com/2015/10/07/mongodb-using-bluebird-promises-with-mongo-db-native-driver-2-0-mongodb-node-driver/ | |
var Promise = require('bluebird'); | |
var dbUrl = 'mongodb://username:[email protected]:port/database-name'; | |
var collectionName = 'MyCollection'; | |
MongoClient | |
.connect(params.dbUrl, { | |
promiseLibrary: Promise | |
}) | |
// TODO: https://www.snip2code.com/Snippet/974049/Algorithm-to-loop-through-a-large-MongoD | |
.then(function(db) { | |
return db | |
.collection(collectionName) | |
.find({}) // find returns cursor | |
.toArray() // cursor methods return promises: http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#toArray | |
.then(function(documents) { | |
logger.log('documents.length', documents.length); | |
return Promise.map( | |
documents || [], | |
function(document){ | |
logger.log(document.name); | |
return db | |
.collection(collectionName) | |
.findOne({_id: document._id}) | |
.then(function(doc) { | |
logger.log('doc:', doc); | |
return Promise.resolve(); | |
}); | |
}, | |
{concurrency: 1} | |
); | |
}) | |
.finally(db.close.bind(db)); | |
}) | |
.catch(function(err) { | |
console.error("ERROR", err); | |
}); |
This file contains hidden or 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
// Inject bluebird as mongo preferred promise library: | |
// https://easyusedev.wordpress.com/2015/10/07/mongodb-using-bluebird-promises-with-mongo-db-native-driver-2-0-mongodb-node-driver/ | |
var Promise = require('bluebird'); | |
var dbUrl = 'mongodb://username:[email protected]:port/database-name'; | |
MongoClient | |
.connect(dbUrl, { | |
promiseLibrary: Promise | |
}) | |
.then(function(db) { | |
return db | |
.collection('ItemModel') | |
.find({}) // find returns cursor | |
.toArray() // cursor methods return promises: http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#toArray | |
.then(function(documents) { | |
logger.log('documents.length', documents.length); | |
}) | |
.finally(db.close.bind(db)); | |
}) | |
.catch(function(err) { | |
console.error("ERROR", err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment