- Reference >
- JavaScript Methods >
- rs.add()
rs.add()¶
-
rs.add(hostspec, arbiterOnly)¶ Specify one of the following forms:
Parameters: - host (string,document) – Either a string or a document. If a string, specifies a host (and optionally port-number) for a new host member for the replica set; MongoDB will add this host with the default configuration. If a document, specifies any attributes about a member of a replica set.
- arbiterOnly (boolean) – Optional. If
true, this host is an arbiter. If the second argument evaluates totrue, as is the case with some documents, then this instance will become an arbiter.
Provides a simple method to add a member to an existing replica set. You can 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 Add a Member to an Existing Replica Set (Alternate Procedure) example.
This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates which node will be primary. As a result, the shell will display an error even if this command succeeds.
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.Example
To add a
mongodaccessible on the default port27017running on the hostmongodb3.example.net, use the followingrs.add()invocation:If
mongodb3.example.netis an arbiter, use the following form:To add
mongodb3.example.netas a secondary-only member of set, use the following form ofrs.add():Replace,
3with the next unused_idvalue in the replica set. Seers.conf()to see the existing_idvalues in the replica set configuration document.See the Replica Set Configuration and Replica Set Operation and Management documents for more information.