If you want to read from a secondary, you should set your read preference to either of:
secondaryPreferred
- In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary.
secondary
- All operations read from the secondary members of the replica set.
Reading from nearest
as per your example will select the nearest member by ping time (which could be either the primary or a secondary).
Caveats
When using any read preference other than primary
, you need to be aware of potential issues with eventual consistency that may affect your application logic. For example, if you are reading from a secondary there may be changes on the primary that have not replicated to that secondary yet.
If you are concerned about stronger consistency when reading from secondaries you should review the Write Concern for Replica Sets documentation.
Since secondaries have to write the same data as the primary, reading from secondaries may not improve performance unless your application is very read heavy or is fine with eventual consistency.