- Reference >
mongo
Shell Methods >- Replication Methods >
- rs.add()
rs.add()¶
On this page
Definition¶
-
rs.
add
(host, arbiterOnly)¶ Adds a member to a 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. If a document, specify a replica-set members document, as found in the members
array. To view a replica set’s members array, runrs.conf()
.arbiterOnly
boolean Optional. Applies only if the <host>
value is a string. Iftrue
, the added host is an arbiter.”You may specify new hosts in one of two ways:
- 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.
- as a configuration document, as in the Configure and Add a Member example.
rs.add()
provides a wrapper around some of the functionality of the “replSetReconfig
” database command and the corresponding shell 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 shell displays
an error even if the operation succeeds.
Example¶
To add a mongod
accessible on the default port
27017
running on the host mongodb3.example.net
, use the
following rs.add()
invocation:
If mongodb3.example.net
is an arbiter, use the following form:
To add mongodb3.example.net
as a secondary-only member of set, use the
following form of rs.add()
:
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 Tutorials documents for more information.