Skip to content

Instantly share code, notes, and snippets.

@straps
Created April 8, 2011 10:27
Show Gist options
  • Save straps/909611 to your computer and use it in GitHub Desktop.
Save straps/909611 to your computer and use it in GitHub Desktop.
Node.js, mongodb and step module integration
/** 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