Connect to MongoDB

Use the client.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';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err) {
  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';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err) {
  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';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(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 assert = require('assert');

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

// Create a client, passing in additional options
const client = new MongoClient(url, {
  tlsCAFile: `${__dirname}/certs/ca.pem`,
  tlsCertificateKeyFile: `${__dirname}/certs/client.pem`
});

// Use connect method to connect to the server
client.connect(function(err) {
  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: