Survey

Please help us improve by answering a few questions at our user survey.

Connect to MongoDB

Note

This reference applies to 2.1.11 or higher. For 2.1.10 or earlier, refer to the legacy connection settings. 2.1.11 is backward compatible with the legacy settings as well as the simplified settings.

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. If the database is omitted, the MongoClient uses the default test database:

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';

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

  db.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, the database to access (myproject), and the name of the replica set (foo). When using the 2.0 driver, you must include the replica set name.

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017,localhost:27018/myproject?replicaSet=foo';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  db.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 and the database to access (myproject).

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:50000,localhost:50001/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  db.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.


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

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

// Connection URL
var 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, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  db.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:

comments powered by Disqus