-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 3.6.4
-
Component/s: None
-
None
When using .setReadPreference() in a query it seems the driver is not routing the reads to secondaries, however if the readPreference is specified in the find() statement the query is then routed to the secondaries.
Please note my comments in the node program as // first method and // second method
Repro:
launching a 2 members replica set
mlaunch --replicaset --nodes 2 --auth
enabling profiler in both instances:
mongo --port 27018 -u user -ppassword --authenticationDatabase admin --eval "rs.secondaryOk(); db.getSiblingDB('test').setProfilingLevel(2);"
mongo --port 27017 -u user -ppassword --authenticationDatabase admin --eval "rs.secondaryOk(); db.getSiblingDB('test').setProfilingLevel(2);"
executing node program
node test.js
querying the profiler collection, each find goes to one instance.
mongo --port 27018 -u user -ppassword --authenticationDatabase admin --eval "rs.secondaryOk(); db.getSiblingDB('test').system.profile.find().sort({ts : 1}).limit(1).pretty();"
mongo --port 27017 -u user -ppassword --authenticationDatabase admin --eval "rs.secondaryOk(); db.getSiblingDB('test').system.profile.find().sort({ts : 1}).limit(1).pretty();"
--- node program --- test.js const { MongoClient, ReadPreference } = require("mongodb"); const uri = "mongodb://user:password@localhost:27017,localhost:27018/test?replSet=repliset&authSource=admin" const client = new MongoClient(uri, { appname: 'mynodeapp', useUnifiedTopology: true, poolSize: 100, serverSelectionTimeoutMS: 60000, socketTimeoutMS: 30000 }); async function run() { try { await client.connect(); var db = client.db("test"); const collection = db.collection('foo') var result = await collection .find({ "readPreference1" : 1 }, {readPreference : ReadPreference.SECONDARY}) // first method .toArray() result = await collection .find({ "readPreference2" : 1 }) .setReadPreference(ReadPreference.SECONDARY) // second method .toArray() console.log("") console.log("finished, please check the system.profile from both primary and secondary") console.log("") } finally { await client.close(); } } run().catch(console.dir);
- related to
-
NODE-3172 Add test for setReadPreference on cursor
- Backlog