Introducing Dragonfly Cloud! Learn More

Question: How do I get replication information in MongoDB?


Getting replication information in MongoDB is crucial for monitoring the status and health of a replica set, which is a group of mongod instances that maintain the same data set. Replica sets provide redundancy and high availability, and are the foundation for all production deployments. Here's how you can obtain replication info:

Using rs.status()

The most common way to get replication info is by using the rs.status() command in the Mongo shell. This command returns a document that contains a lot of detailed information about the state of the replica set, including the status of each member.


This will return a variety of details, such as:

  • The set name of the replica set.
  • The date when the status was retrieved.
  • An array of members, with each member containing fields like name (host:port), stateStr (e.g., PRIMARY, SECONDARY), optime (the timestamp of the last operation applied), and many others.

Using db.adminCommand()

For more specific replication information, you can use the db.adminCommand() function with different commands. For example, replSetGetStatus can be used similarly to rs.status() but executed through db.adminCommand().

db.adminCommand({ replSetGetStatus: 1 })

Checking Replication Lag

One important aspect of managing a replica set is monitoring the replication lag, which is the delay between an operation on the primary and the application of that operation on a secondary. You can compute the replication lag by comparing the optime values of the members in the output of rs.status().

Keep in mind that interpreting the results from these commands requires understanding the structure and roles within a replica set (e.g., primary, secondary, arbiter) and how MongoDB replication works.

These methods provide essential insights into the health and performance of MongoDB replica sets and are invaluable for database administration and troubleshooting.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.