How do I connect to mongodb with node.js (and authenticate)?

Mark picture Mark · Jan 14, 2011 · Viewed 108k times · Source

How do I connect to mongodb with node.js?

I have the node-mongodb-native driver.

There's apparently 0 documentation.

Is it something like this?

var mongo = require('mongodb/lib/mongodb'); 
var Db= new mongo.Db( dbname, new mongo.Server( 'mongolab.com', 27017, {}), {}); 

Where do I put the username and the password?

Also how do I insert something?

Thanks.

Answer

Chris Heald picture Chris Heald · Jan 14, 2011

Per the source:

After connecting:

Db.authenticate(user, password, function(err, res) {
  // callback
});