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

Update API Documentation to properly document BulkOp usage with Sessions

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: