Javatpoint Logo
Javatpoint Logo

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.

Example:

In the following example we will add a new secondary member with default vote.

Host name


MongoDB Replication Methods

#2. rs.addArb(host)

We can add a new arbiter to an existing replica set at the specified host.

Example

Adding an arbiter to the Associated-Hostname

If you want to connect to this instance, the hostname or its associated IP address 198.52.100.2 must be specified by the remote clients:


MongoDB Replication Methods

#3. rs.conf()

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.

Example

The following script shows the configuration document of a replica set that include the subset of these settings:

Output:

MongoDB Replication Methods

#4. rs.initiate(configuration)

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.

Example

The following example initiates a new replica set with three different members.

Output:

MongoDB Replication Methods

#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.

Examples

A replica set named rs0 has the following configuration:


Next TopicMongoDB Stitch





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA