Created
April 8, 2011 10:27
-
-
Save straps/909611 to your computer and use it in GitHub Desktop.
Node.js, mongodb and step module integration
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
/** Use Step to interact with a MongoDB database avoiding too many confusing callbacks, | |
requires: npm install mongodb step | |
*/ | |
var mongo = require('mongodb'), | |
db=new mongo.Db('test', new mongo.Server('localhost', 27017, {}), {}), | |
step=require('step'), | |
col; | |
step( | |
function open(){ | |
db.open(this); | |
}, | |
function onOpen(err){ | |
if (err) throw err; | |
db.collection('test1', this); | |
}, | |
function onCollection(err, rv){ | |
if (err) throw err; | |
col=rv; | |
col.insert({name:'Record 1 Added via Node and Step', d:new Date()}, this.parallel()); | |
col.insert({name:'Record 2 Added via Node and Step in parallel', d:new Date()}, this.parallel()); | |
}, | |
function onInsert(err){ | |
if (err) throw err; | |
col.find(this); | |
}, | |
function onFind(err, rv){ | |
if (err) throw err; | |
db.close(); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment