MongoDB Replication Methods
The MongoDB Replication methods are used to replicate the member to the replica sets.
#1. rs.add(host, arbiterOnly)
The add method adds a member to the specified replica set. We are required to connect to the primary set of the replica set to this method. The connection to the shell will be terminated if the method will trigger an election for primary. For example - if we try to add a new member with a higher priority than the primary. An error will be reflected by the mongo shell even if the operation succeeds.
In the following example we will add a new secondary member with default vote.
We can add a new arbiter to an existing replica set at the specified host.
Adding an arbiter to the Associated-Hostname
If you want to connect to this instance, the hostname or its associated IP address 188.8.131.52 must be specified by the remote clients:
The conf method is used to get a document that contains the configuration of the current replica set.
The rs.conf() wraps the replSetGetConfig command configuration.
The following script shows the configuration document of a replica set that include the subset of these settings:
The method initiates a replica set. A document that holds the rs.initiate() configuration of a replica set can be considered by this method, but that is optional.
The following example initiates a new replica set with three different members.
#5. rs.reconfig(configuration, force)
As the name suggests, the method is used to reconfigure an existing replica set. It will overwrite all the existing replica set configuration. We must connect to the primary replica set to run this method.
A replica set named rs0 has the following configuration: