- Reference >
mongoShell Methods >- Replication Methods >
- rs.add()
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 hoststring 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
membersarray. You must specify_idand thehostfields 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
arbiterOnlyboolean Optional. Applies only if the <host>value is a string. Iftrue, the added host is an arbiter.rs.add()provides a wrapper around some of the functionality of thereplSetReconfigdatabase command and the correspondingmongoshell helperrs.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:
If mongodb3.example.net is an arbiter, use the following form:
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:
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.