Mongodb – Add a mongodb read only replica for reporting purpose

mongodbmongodb-3.2

I have a MongoDB DB X sharded in 2 replica set. Now I want to add a replica (which will be only read operations) to reduce load on mongodb for reporting purpose.
How is possible to achieve this as my collections are sharded to 2 different replica sets?

Best Answer

Sharding a Mongo DB should not have any impact on your readable secondaries. Each shard will have a replica set just as if it wasn't sharded. The 'read only' methods to access those would be the same way.

https://docs.mongodb.com/manual/core/read-preference-mechanics/ From the link:

Read Preference and Shards

For sharded clusters that have replica set shards, mongos applies the read preference when reading from the shards. Server selection is governed by the read preference and replication.localPingThresholdMs settings.

You're probably going to look for the 'secondary preferred' option: https://docs.mongodb.com/manual/reference/read-preference/#secondaryPreferred

"In most situations, operations read from secondary members, but in situations where the set consists of a single primary (and no other members), the read operation will use the set’s primary. When the read preference includes a tag set, the client attempts to find a secondary member that matches the specified tag set and directs reads to a random secondary from among the nearest group. If no secondaries have matching tags, the client ignores tags and reads from the primary. Read operations using the secondaryPreferred mode may return stale data."