rs.status()
The rs.status() command returns a document with a members array where each member has a stateStr field. The member with stateStr equal to 'PRIMARY' is the current primary member. The primary field is not part of rs.status() output but is returned by rs.isMaster().
MongoDB allows up to 7 voting members in a replica set to maintain a stable election process and fault tolerance. Having more than 7 voting members is not supported.
var cfg = rs.conf(); cfg.members.push({ _id: 2, host: 'mongo3.example.net:27017' }); rs.reconfig(cfg);
The rs.conf() method returns the current replica set configuration. To add a member, you push a new object to the members array. Then you call rs.reconfig() with the updated config. Using rs.status() or incorrect methods like add or pushing directly to cfg will cause errors.
var cfg = rs.conf(); cfg.members.push({ _id: 1, host: 'mongo3.example.net:27017' }); rs.reconfig(cfg);
Each member in a replica set configuration must have a unique _id. Using an _id that already exists causes the error. The host field format is correct, and force is not required unless the primary is down. Cloning is not mandatory.
Setting a member's priority to 0 means it cannot become primary but still participates in voting during elections. To prevent voting, the votes field must be set to 0.