liveBook · Manning

Rs.status()

As explained in MongoDB 8.0 in Action, Third Edition

rs.status() is a MongoDB shell method that returns the current status of replica-set members and replication.

rs.status() is a mongosh helper for inspecting the current health, roles, and replication state of MongoDB replica-set members. It provides a script-friendly alternative to db.printSecondaryReplicationInfo() and helps identify primary, secondary, unreachable, synchronizing, or rollback states. section 9.1.1 section 9.1.3

Use the returned member information to:

  • Confirm which member is PRIMARY and which members are SECONDARY. section 9.1.1
  • Detect STARTUP, STARTUP2, RECOVERING, DOWN, UNKNOWN, ROLLBACK, or REMOVED conditions. section 9.1.1
  • Check whether secondaries are synchronized with the primary and identify replication lag. section 9.1.3

Inspect member states

Run rs.status() in mongosh, or extract a compact result with db.adminCommand("replSetGetStatus").members.map((m) => ({ _id: m._id, name: m.name, state: m.state, stateStr: m.stateStr })). A typical three-member result shows one PRIMARY and two SECONDARY members. section 9.1.1

Listing 9.1 Output of the replSetGetStatus command
db.adminCommand("replSetGetStatus").members.map((m) =>
➥({ _id: m._id, name: m.name, state: m.state, stateStr: m.stateStr }))
[
 {
  _id: 0,
  name: 'ac-5dhjxpf-shard-00-00.fpomkke.mongodb.net:27017'
  state: 2,
  stateStr: 'SECONDARY'
 },
 {
  _id: 1,
  name: 'ac-5dhjxpf-shard-00-01.fpomkke.mongodb.net:27017'
  state: 1,
  stateStr: 'PRIMARY'
 },
 {
  _id: 2,
  name: 'ac-5dhjxpf-shard-00-02.fpomkke.mongodb.net:27017'
  state: 2,
  stateStr: 'SECONDARY'
 }
]

Browse a compact status projection showing each member's identifier, host, numeric state, and readable state name. section 9.1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains member states and synchronization checks, but defers detailed replica-set configuration to rs.config().
Examples
Demonstrates mongosh status projections and reports secondaries with zero- and ten-second replication lag.
What it assumes
Assumes familiarity with MongoDB replica sets and mongosh while defining member roles and possible states.
What you can do after
Diagnose replica-set member conditions and automate checks of roles, reachability, synchronization, and replication lag.
How it connects
Connects replica sets with primary, secondary, replication lag, oplog, rollback, and arbiter concepts.

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.