|
I have a sharded MongoDB cluster which has 2 shards. Each shard has 1 primary node, 1 secondary node and 1 arbiter. The mongos runs on one of the cluster machines.
I use the following codes to connec to mongos:
Mongo mongo = new Mongo(args[0]);
DB emData = mongo.getDB(args[1]);
DBCollection publicationMonth = emData.getCollection(args[2]);
publicationMonth.setReadPreference(ReadPreference.secondary());
However, when I execute an aggregation on the "publicationMonth" collection, only the primary nodes are busy to execute the query while the secondary nodes sitting there idle.
If I modify above codes a little bit to:
Mongo mongo = new Mongo(args[0]);
mongo.setReadReference(ReadPreference.secondary());
DB emData = mongo.getDB(args[1]);
DBCollection publicationMonth = emData.getCollection(args[2]);
secondary nodes will execute the aggregation query instead of primary nodes.
|