How to Query MongoDB Secondary Replica Set Members
In this article, we learn how to query a MongoDB from a non-master (slave or secondary) replica set member.
Applies to:
- MongoDB replica set
Problem
When issuing a simple query on a non-master member, the user will see the following error:
What Causes This Error
This is because, by default, MongoDB does not permit read operations from a non-master member. In MongoDB, reads to a primary provides data that reflects the latest writes at all times. However, secondaries are not required to reflect the latest writes at all times and changes to the system can propagate gradually.
How to Fix the Error
By running rs.slaveOk(), you are indicating to the system that you acknowledge that reads will eventually be consistent and you wish to be able to query the secondary anyway.
You learned the steps to "Query MongoDB Secondary Replica Set Members".