How to connect to mongodb using node.js (and authenticate)?

How to connect to mongodb using node.js?

I have a native node-mongodb driver.

There, apparently, is 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 can I put my username and password?

Also, how do I insert something?

Thank.

+59
javascript authentication mongodb connection
Jan 14 '11 at 7:01
source share
13 answers

Per source :

After connecting:

 Db.authenticate(user, password, function(err, res) { // callback }); 
+40
Jan 14 '11 at 7:10
source share

Everyone should use this link:

http://mongodb.github.com/node-mongodb-native/contents.html

Answer the question:

 var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('integration_tests', new Server("127.0.0.1", 27017, {auto_reconnect: false, poolSize: 4}), {w:0, native_parser: false}); // Establish connection to db db.open(function(err, db) { assert.equal(null, err); // Add a user to the database db.addUser('user', 'name', function(err, result) { assert.equal(null, err); // Authenticate db.authenticate('user', 'name', function(err, result) { assert.equal(true, result); db.close(); }); }); }); 
+30
Mar 03 '13 at 21:45
source share

I am using Mongo url. I save the URL in an environment variable and use it to configure the servers, while the development version uses the default URL without a password.

The URL has the form:

 export MONGODB_DATABASE_URL=mongodb://USERNAME:PASSWORD@DBHOST:DBPORT/DBNAME 

The code to connect is as follows:

 var DATABASE_URL = process.env.MONGODB_DATABASE_URL || mongodb.DEFAULT_URL; mongo_connect(DATABASE_URL, mongodb_server_options, function(err, db) { if(db && !err) { console.log("connected to mongodb" + " " + lobby_db); } else if(err) { console.log("NOT connected to mongodb " + err + " " + lobby_db); } }); 
+7
Jul 26 2018-12-12T00:
source share
 var mongo = require('mongodb'); var MongoClient = mongo.MongoClient; MongoClient.connect('mongodb://'+DATABASEUSERNAME+':'+DATABASEPASSWORD+'@'+DATABASEHOST+':'DATABASEPORT+'/'+DATABASENAME,function(err, db){ if(err) console.log(err); else { console.log('Mongo Conn....'); } }); //for local server //in local server DBPASSWOAD and DBusername not required MongoClient.connect('mongodb://'+DATABASEHOST+':'+DATABASEPORT+'/'+DATABASENAME,function(err, db){ if(err) console.log(err); else { console.log('Mongo Conn....'); } }); 
+7
Oct 18 '16 at 12:34
source share

My version:

 var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://user:pass@dhost:port/baseName', function(err, db) { if (err) { console.error(err); } var collection = db.collection('collectionName'); collection.find().toArray(function(err, docs) { console.log(docs); }); }); 
+5
Jan 22 '15 at 5:51 on
source share

I recommend mongoskin I just created.

 var mongo = require('mongoskin'); var db = mongo.db('admin:pass@localhost/mydb?auto_reconnnect'); db.collection('mycollection').find().toArray(function(err, items){ // do something with items }); 

Is Mongoskin synchronization? Nop, this is asynchronous.

+3
Apr 08 2018-11-11T00:
source share

This worked for me:

 Db.admin().authenticate(user, password, function() {} ); 
+3
Oct 08 2018-11-11T00:
source share

You can do it like this:

 var db = require('mongo-lite').connect('mongodb://localhost/test') 

more details ...

+2
Jan 21 '12 at 23:21
source share

if you still have problems with the native driver, you can also check the sleepy mongoose. This is a python REST server with which you can simply access with a node request to get to your Mongo instance. http://www.snailinaturtleneck.com/blog/2010/02/22/sleepy-mongoose-a-mongodb-rest-interface/

+1
Jan 14 2018-11-11T00:
source share

With the link provided by @mattdlockyer as a link, this worked for me:

 var mongo = require('mongodb'); var server = new mongo.Server(host, port, options); db = new mongo.Db(mydb, server, {fsync:true}); db.open(function(err, db) { if(!err) { console.log("Connected to database"); db.authenticate(user, password, function(err, res) { if(!err) { console.log("Authenticated"); } else { console.log("Error in authentication."); console.log(err); } }); } else { console.log("Error in open()."); console.log(err); }; }); exports.testMongo = function(req, res){ db.collection( mycollection, function(err, collection) { collection.find().toArray(function(err, items) { res.send(items); }); }); }; 
+1
Jun 19 '13 at 3:59
source share

A minor typo with Chris's answer.

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

it should be

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

In addition, depending on the configuration of your mongodb, you may need to first connect to admin and auth before moving to another database. This will happen if you do not add the user to the database that you are trying to access. You can then authorize via admin, and then switch db and then read or write as you wish.

0
May 29 '12 at 18:04
source share

I am using Mongoose to connect to mongodb. Install mongoose npm using the following command

npm install mongoose

 var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/database_name', function(err){ if(err){ console.log('database not connected'); } }); var Schema = mongoose.Schema; var userschema = new Schema ({}); var user = mongoose.model('collection_name', userschema); 

we can use queries like

 user.find({},function(err,data){ if(err){ console.log(err); } console.log(data); }); 
0
Sep 08 '14 at
source share

A new authentication feature may appear here with "admin", and then go to the required database for further operations:

  var MongoClient = require('mongodb').MongoClient; var Db = require('mongodb').Db, Server = require('mongodb').Server , assert = require('assert'); var user = 'user'; var password = 'password'; MongoClient.connect('mongodb://'+user+':'+password+'@localhost:27017/opsdb',{native_parser:true, authSource:'admin'}, function(err,db){ if(err){ console.log("Auth Failed"); return; } console.log("Connected"); db.collection("cols").find({loc:{ $eq: null } }, function(err, docs) { docs.each(function(err, doc) { if(doc) { console.log(doc['_id']); } }); }); db.close(); }); 
0
Oct 16 '17 at 9:52 on
source share



All Articles