Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3129

Collection () .. .setReadPreference() not routing query to secondaries

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.6
    • Affects Version/s: 3.6.4
    • Component/s: None
    • None
    • Not Needed

       

      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);
      
      

       

            Assignee:
            eric.adum@mongodb.com Eric Adum (Inactive)
            Reporter:
            adamo.tonete@mongodb.com Adamo Tonete (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: