Navigation
This version of the documentation is archived and no longer supported.

rs.add()

rs.add(hostspec, arbiterOnly)

Specify one of the following forms:

Parameters:
  • host (string,document) – Either a string or a document. If a string, specifies a host (and optionally port-number) for a new host member for the replica set; MongoDB will add this host with the default configuration. If a document, specifies any attributes about a member of a replica set.
  • arbiterOnly (boolean) – Optional. If true, this host is an arbiter. If the second argument evaluates to true, as is the case with some documents, then this instance will become an arbiter.

Provides a simple method to add a member to an existing replica set. You can specify new hosts in one of two ways:

  1. as a “hostname” with an optional port number to use the default configuration as in the Add a Member to an Existing Replica Set example.
  2. as a configuration document, as in the Add a Member to an Existing Replica Set (Alternate Procedure) example.

This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates which node will be primary. As a result, the shell will display an error even if this command succeeds.

rs.add() provides a wrapper around some of the functionality of the “replSetReconfigdatabase command and the corresponding shell helper rs.reconfig(). See the Replica Set Configuration document for full documentation of all replica set configuration options.

Example

To add a mongod accessible on the default port 27017 running on the host mongodb3.example.net, use the following rs.add() invocation:

rs.add('mongodb3.example.net:27017')

If mongodb3.example.net is an arbiter, use the following form:

rs.add('mongodb3.example.net:27017', true)

To add mongodb3.example.net as a secondary-only member of set, use the following form of rs.add():

rs.add( { "_id": 3, "host": "mongodbd3.example.net:27017", "priority": 0 } )

Replace, 3 with the next unused _id value in the replica set. See rs.conf() to see the existing _id values in the replica set configuration document.

See the Replica Set Configuration and Replica Set Operation and Management documents for more information.

←   resetDbpath() rs.addArb()  →