-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: 3.0.0, 3.0.2
-
Component/s: Command Operations
-
None
Hi,
It looks like the readPreference for a db command is not always working as expected.
1 case:
readPreference needs to be set as a document option even if already set in the connection URL. i.e.
MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017/myDB?w=2&readPreference=nearest&maxPoolSize=1100");
This is the document used:
Document point = new Document("type", "Point").append("coordinates", coordinates); Document geoQuery = new Document(); geoQuery.append("geoNear", "places"); geoQuery.append("near", point); geoQuery.append("query", condition); geoQuery.append("spherical", "true"); geoQuery.append("limit", 1000); geoQuery.append("maxDistance", 50000); //geoQuery.append("$readPreference", new Document("mode", "nearest"));
If this {{//geoQuery.append("$readPreference", new Document("mode", "nearest")) }} is commented, the queries will always go to the primary.
If it is not commented it would balance across primary and secondaries based on ping (as expected by using nearest).
2nd case:
With the following code, readPreference set to nearest seems to work as expected.
MongoClient m = new MongoClient(new MongoClientURI("mongodb://localhost:37019/sprint?w=2&readPreference=nearest&maxPoolSize=1100"));
BasicDBObject myCmd = new BasicDBObject(); myCmd.append("geoNear", "places"); myCmd.append("type", "Point"); double[] loc = {101.7252, 3.1141}; myCmd.append("near", loc); myCmd.append("spherical", true); myCmd.append("maxDistance", 100000); CommandResult r = db.command(myCmd);
The node performing the queries is checked by using db.setLogLevel(3) and looking at the commands on each node.
If any other information is required please let me know.
Thank you,
Regards,
Marco