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

Update API Documentation to properly document BulkOp usage with Sessions

    • Type: Icon: Task Task
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Docs
    • None
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      The NodeJs Driver documentation for initializeOrderedBulkOp and initializeUnorderedBulkOp both show the method params as supporting a ClientSession as an option.

      If attempting to leverage BulkOps and Transactions via the Node driver, the expected behaviour (such as cancelling the transaction) won't apply as the session is not being properly handled.

      For example the following will fail to cancel the transaction and the insert will still be executed:

      const assert = require('assert');
      const { MongoClient } = require('mongodb');
      const MONGODB_URI = 'mongodb://localhost:27017/test';
      
      async function main() {
        const client = await MongoClient.connect(MONGODB_URI, {
          useUnifiedTopology: true,
        });
        const db = client.db();
        const collection = db.collection('bulk');
        await collection.deleteMany({});
      
        const session = client.startSession();
        await session.startTransaction();
      
        const bulk = collection.initializeUnorderedBulkOp({session});
        bulk.insert({ bulk: true });
        bulk.execute();
      
        await session.abortTransaction();
      
        const results = await collection.find({}).toArray();
        await client.close();
      
        assert.equal(results.length, 0, "Expected 0 documents")
      }
      main();
      

      The "fix" is to move the session to the execute method of the Bulk operation:

      // move the session from the BulkOp initialization
      const bulk = collection.initializeUnorderedBulkOp({session});
      bulk.execute();
      // to the BulkOp execution
      const bulk = collection.initializeUnorderedBulkOp();
      bulk.execute(null, {session});
      

      This was flagged as an issue In NODE-1843 originally. This task is to adjust the documentation until NODE-1843 is addressed as the workaround is not documented.

            Assignee:
            Unassigned Unassigned
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: