Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 536
Mark Avatar asked Jan 14 '11 07:01

Mark


People also ask

How do I authenticate a user in MongoDB?

To authenticate as a user, you must provide a username, password, and the authentication database associated with that user. To authenticate using the mongo shell, either: Connect first to the MongoDB or mongos instance. Run the authenticate command or the db.


1 Answers

Per the source:

After connecting:

Db.authenticate(user, password, function(err, res) {   // callback }); 
like image 162
Chris Heald Avatar answered Sep 28 '22 21:09

Chris Heald