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

Driver 3.5.x with useUnifiedTopology sends admin commands to secondary

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.5.10
    • Affects Version/s: 3.5.8
    • Component/s: None
    • Labels:
      None

      When using 3.5.8 driver with secondaryPreferred and useUnifiedTopology the driver seems to send admin commands like collMod to a secondary.

      const { MongoClient } = require('mongodb');
      
      const url = 'mongodb://localhost/?replicaSet=replset&authSource=admin&readPreference=secondaryPreferred&retryWrites=true';
      
      (async () => {
          const client = await MongoClient.connect(url , { useUnifiedTopology: true })
          const db = await client.db('media-providers');
      
          try {
              await db.collection('tests', { strict: true });
          } catch (err) {
              await db.createCollection('tests');
          }
      
          try {
              const result = await db.command({
                  collMod: 'tests',
                  validator: {
                      $jsonSchema: {
                          type: 'object',
                          properties: {
                              score: { type: 'number' }
                          }
                      }
                  }
              });
              console.log('result', result);
          } catch (err) {
              console.error(err);
          }
      })();
      

      this gives :

      MongoError: not master
          at MessageStream.messageHandler (/home/dev/node_modules/mongodb/lib/cmap/connection.js:261:20)
          at MessageStream.emit (events.js:210:5)
          at processIncomingData (/home/dev/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
          at MessageStream._write (/home/dev/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
          at doWrite (_stream_writable.js:431:12)
          at writeOrBuffer (_stream_writable.js:415:5)
          at MessageStream.Writable.write (_stream_writable.js:305:11)
          at TLSSocket.ondata (_stream_readable.js:727:22)
          at TLSSocket.emit (events.js:210:5)
          at addChunk (_stream_readable.js:309:12) {
        operationTime: Timestamp { _bsontype: 'Timestamp', low_: 30, high_: 1591872518 },
        ok: 0,
        code: 10107,
        codeName: 'NotMaster',
        '$clusterTime': {
          clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1591872519 },
          signature: { hash: [Binary], keyId: [Long] }
        },
        name: 'MongoError'
      }
      

      Setting readPreference to primaryPreferred OR removing

      { useUnifiedTopology: true }

      resolves the problem (seemingly by sending the administrative command to the primary as expected).

            Assignee:
            thomas.reggi@mongodb.com Thomas Reggi (Inactive)
            Reporter:
            nicholas.cottrell@mongodb.com Nic Cottrell
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: