21

The MongoClient documentation shows how to use a Server instance to create a connection:

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server;

// Set up the connection to the local db
var mongoclient = new MongoClient(new Server("localhost", 27017));

How would you specify a username and password for this?

Oved D
  • 7,132
  • 10
  • 47
  • 69

2 Answers2

37

There are two different ways you can do this

#1

Documentation(Note the example in the documentation uses the Db object)

// Your code from the question

// Listen for when the mongoclient is connected
mongoclient.open(function(err, mongoclient) {

  // Then select a database
  var db = mongoclient.db("exampledatabase");

  // Then you can authorize your self
  db.authenticate('username', 'password', function(err, result) {
    // On authorized result=true
    // Not authorized result=false

    // If authorized you can use the database in the db variable
  });
});

#2

Documentation MongoClient.connect
Documentation The URL
A way I like much more because it is smaller and easier to read.

// Just this code nothing more

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://username:password@localhost:27017/exampledatabase", function(err, db) {
  // Now you can use the database in the db variable
});
Mattias
  • 9,211
  • 3
  • 42
  • 42
  • 2
    Yeah, after some digging around it seems like the only way to authenticate is on the db level, not the server. So this makes sense. I went with #2. – Oved D Dec 27 '12 at 13:56
  • @Mattias - If get a change please check this - https://stackoverflow.com/questions/54442315/nodejs-mongno-db-connect-with-server-database-with-username-and-password – RSKMR Jan 30 '19 at 14:58
5

Thanks Mattias for correct answer.

I would like to add that sometimes you have credentials from one database while want to connect to another. In that case, you can still use URL way to connect, just adding ?authSource= parameter to URL.

For example, let say you have admin credentials from database admin, and want to connect to database mydb. You can do it the following way:

const MongoClient = require('mongodb').MongoClient;

(async() => {

    const db = await MongoClient.connect('mongodb://adminUsername:adminPassword@localhost:27017/mydb?authSource=admin');

    // now you can use db:
    const collection = await db.collection('mycollection');
    const records = await collection.find().toArray();
    ...

})();

Also, if your password contains special characters, you still can use URL way like this:

    const dbUrl = `mongodb://adminUsername:${encodeURIComponent(adminPassword)}@localhost:27017/mydb?authSource=admin`;
    const db = await MongoClient.connect(dbUrl);

Note: In earlier versions, { uri_decode_auth: true } option was required (as second parameter to connect method) when using encodeURIComponent for username or password, however now this option is obsolete, it works fine without it.

Maxím G.
  • 860
  • 10
  • 14