OPTIONS

rs.reconfig()

Definition

rs.reconfig(configuration, force)

Reconfigures an existing replica set, overwriting the existing replica set configuration. To run the method, you must connect to the primary of the replica set.

Parameter Type Description
configuration document A document that specifies the configuration of a replica set.
force document Optional. If set as { force: true }, this forces the replica set to accept the new configuration even if a majority of the members are not accessible. Use with caution, as this can lead to rollback situations.

When used to reconfigure an existing replica set, first retrieve the current configuration with rs.conf(), modify the configuration document as needed, and then pass the modified document to rs.reconfig().

rs.reconfig() provides a wrapper around the replSetReconfig database command.

Behavior

The method disconnects the mongo shell and forces a reconnection as the replica set renegotiates which member will be primary. As a result, the shell will display an error even if this command succeeds.

The rs.reconfig() shell method can force the current primary to step down and triggers an election in some situations. When the primary steps down, the primary closes all client connections. This is by design. Since this typically takes 10-20 seconds, attempt to make such changes during scheduled maintenance periods.

Warning

Using rs.reconfig() with { force: true } can lead to rollback situations and other difficult-to-recover-from situations. Exercise caution when using this option.

Examples

A replica set named rs0 has the following configuration:

{
    "_id" : "rs0",
    "version" : 1,
    "members" : [
         {
            "_id" : 0,
            "host" : "mongodb0.example.net:27017"
         },
         {
            "_id" : 1,
            "host" : "mongodb1.example.net:27017"
         },
         {
            "_id" : 2,
            "host" : "mongodb2.example.net:27017"
         }
     ]
}

The following sequence of operations updates the priority of the second member. The operations are issued through a mongo shell connected to the primary.

cfg = rs.conf();
cfg.members[1].priority = 2;
rs.reconfig(cfg);
  1. The first statement uses the rs.conf() method to retrieve a document containing the current configuration for the replica set and sets the document to the local variable cfg.

  2. The second statement sets a priority value to the second document in the members array. For additional settings, see replica set configuration settings.

    To access the member configuration document in the array, the statement uses the array index and not the replica set member’s _id field.

  3. The last statement calls the rs.reconfig() method with the modified cfg to initialize this new configuration. Upon successful reconfiguration, the replica set configuration will resemble the following:

{
    "_id" : "rs0",
    "version" : 2,
    "members" : [
         {
            "_id" : 0,
            "host" : "mongodb0.example.net:27017"
         },
         {
            "_id" : 1,
            "host" : "mongodb1.example.net:27017",
            "priority" : 2
         },
         {
            "_id" : 2,
            "host" : "mongodb2.example.net:27017"
         }
     ]
}