- Reference >
mongo
Shell 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 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 thehost
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. Iftrue
, the added host is an arbiter.rs.add()
provides a wrapper around some of the functionality of thereplSetReconfig
database command and the correspondingmongo
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, trigger 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()
¶
Changed in version 3.0.0: Previous versions required an _id
field in the document you
passed to rs.add()
. After 3.0.0 you can omit the
_id
field in this document.
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 the
host
field in the member
configuration document.
See the Replica Set Configuration for the available replica set member configuration settings.
See Replica Set Tutorials for more examples and information.