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

rs.add()

On this page

Definition

rs.add()

Adds a member to a replica set. To run the method, you must connect to the primary of the replica set.

Parameter Type Description
host string or document

The new member to add to the replica set.

If a string, specify the hostname and optionally the port number for the new member. See Pass a Hostname String to rs.add() for an example.

If a document, specify a replica set member configuration document as found in the members array. You must specify _id and the host fields in the member configuration document. See Pass a Member Configuration Document to rs.add() for an example.

See Replica Set Configuration document for full documentation of all replica set configuration options

arbiterOnly boolean Optional. Applies only if the <host> value is a string. If true, the added host is an arbiter.

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

Behavior

rs.add() can, in some cases, force an election for primary which will disconnect the shell. In such cases, the mongo shell displays an error even if the operation succeeds.

Example

Pass a Hostname String to rs.add()

The following operation adds a mongod instance, running on the host mongodb3.example.net and accessible on the default port 27017:

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

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

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

Pass a Member Configuration Document to rs.add()

The following operation adds a mongod instance, running on the host mongodb4.example.net and accessible on the default port 27017, as a priority 0 secondary member:

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

You must specify _id and the host fields in the member configuration document.

The _id value must be unique. Use rs.conf() to see the existing _id values in the replica set configuration document, and set _id to the next unused _id value in the replica set.

See the Replica Set Configuration for the available replica set member configuration settings.

See Replica Set Tutorials for more examples and information.