Connect to MongoDB

Use the MongoClient.connect method to connect to a running MongoDB deployment.

Connect to a Single MongoDB Instance

To connect to a single MongoDB instance, specify the URI of the MongoDB instance to connect to.

In the following example, the URI connection string specifies connecting to a MongoDB instance that is running on localhost using port 27017. The myproject indicates the database to use.

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

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

For more information on the URI connection string, see URI Connection String .

Connect to a Replica Set

To connect to a replica set, include a seedlist of replica set members and the name of the replica set in the URI connection string.

In the following example, the connection string specifies two of the replica set members running on localhost:27017 and localhost:27018 and the name of the replica set (foo).

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

// Connection URL
const url = 'mongodb://localhost:27017,localhost:27018/?replicaSet=foo';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);

  client.close();
});

For more information on the URI connection string, see URI Connection String .

Connect to Sharded Cluster

To connect to a sharded cluster, specify the mongos instance or instances in the URI connection string.

In the following example, the connection string specifies the mongos instances running on localhost:50000 and localhost:50001.

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

// Connection URL
const url = 'mongodb://localhost:50000,localhost:50001';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

For more information on the URI connection string, see URI Connection String .

Connection Options

You can specify various connection settings in the URI Connection String .

For example, you can specify TLS/SSL and authentication setting.


const MongoClient = require('mongodb').MongoClient;
const f = require('util').format;
const assert = require('assert');
const fs = require('fs');

  // Read the certificate authority
const ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
const cert = fs.readFileSync(__dirname + "/ssl/client.pem");

// Connection URL
const url = 'mongodb://dave:password@localhost:27017?authMechanism=DEFAULT&authSource=db&ssl=true"';

// Use connect method to connect to the Server passing in
// additional options
MongoClient.connect(url,  {
  server: {
      sslValidate:true
    , sslCA:ca
    , sslCert:cert
  }
}, function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  client.close();
});

For more information on connecting with authentication and TSL/SSL, see:

  • Authentication: detailed documentation of the various ways to specify authentication credentials
  • TLS/SSL: Detailed documentation of the various ways to specify the properties of an TLS/SSL connection

For more information on the connection options: