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

Only the first command in a transaction may specify a readConcern

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      Transaction fails on with error Only the first command in a transaction may specify a readConcern, when second or next command is reading (find, findOne, etc.).

       

      Test sample:

      const assert = require('assert');
      const mongodb = require('mongodb');
      const url = 'mongodb://mongo:27017,mongo:27018,mongo:27019/test?replicaSet=rs';
      const options = {
          useNewUrlParser: true,
          readConcern: {
              level: 'majority'
          }
      };
      
      mongodb.connect(url, options, (error, client) => {
          assert.ifError(error);
      
          const db = client.db();
          const admin = new mongodb.Admin(db, client.topology);
      
          admin.serverInfo((error, info) => {
              assert.ifError(error);
              console.log('nodejs version: ' + client.topology.clientInfo.platform);
              console.log('client version: ' + client.topology.clientInfo.driver.version);
              console.log('server version: ' + info.version + ' (git: ' + info.gitVersion + ')');
      
              const session = client.startSession();
              const collection = client.db().collection('things');
              const options = {session: session};
      
              session.startTransaction();
              collection.insertOne({name: 'something'}, options, (error) => {
                  assert.ifError(error);
                  collection.findOne({name: 'something'}, options, (error, thing) => {
                      assert.ifError(error);
                  });
              });
          });
      });
      

      Test output:

      nodejs version: Node.js v10.13.0, LE
      client version: 3.1.10
      server version: 4.0.4 (git: f288a3bdf201007f3693c58e140056adf8b04839)
      /app/node_modules/mongodb/lib/utils.js:132
            throw err;
            ^AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Only the first command in a transaction may specify a readConcern
          at collection.findOne (/app/testcase.js:31:24)
          at err (/app/node_modules/mongodb/lib/utils.js:415:14)
          at executeCallback (/app/node_modules/mongodb/lib/utils.js:404:25)
          at handleCallback (/app/node_modules/mongodb/lib/utils.js:128:55)
          at cursor.next (/app/node_modules/mongodb/lib/operations/collection_ops.js:582:29)
          at err (/app/node_modules/mongodb/lib/utils.js:415:14)
          at executeCallback (/app/node_modules/mongodb/lib/utils.js:404:25)
          at handleCallback (/app/node_modules/mongodb/lib/utils.js:128:55)
          at cursor._next (/app/node_modules/mongodb/lib/operations/cursor_ops.js:186:21)
          at handleCallback (/app/node_modules/mongodb-core/lib/cursor.js:203:5)
          at queryCallback (/app/node_modules/mongodb-core/lib/cursor.js:248:25)
          at /app/node_modules/mongodb-core/lib/connection/pool.js:532:18
          at process._tickCallback (internal/process/next_tick.js:61:11)
      

            Assignee:
            katherine.walker@mongodb.com Katherine Walker (Inactive)
            Reporter:
            sempasha@gmail.com Pasha Semenov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: