- 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 members[n]._id and the members[n].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, 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:
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()¶
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. members[n]._id describes the requirements for specifying _id.
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( { host: "mongodbd4.example.net:27017", priority: 0 } )
You must specify the members[n].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.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.