-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 3.1.12
-
Component/s: Shell
Hello,
Bulk operation created via initializeOrderedBulkOp or initializeUnorderedBulkOp does not support MongoDB transactions. According to the documentation a client session (with started transaction) can be passed as an option to the collection methods mentioned above, however created bulk operations do not seem to be executed within a passed session transaction. Please review the test case below (also added to the issue attachments).
"use strict"; const { expect } = require ('chai') const { MongoClient } = require ('mongodb') let client = undefined describe ('Bulk operation transaction rollback', () => { before (async () => client = await MongoClient.connect ('mongodb://localhost:27017', { useNewUrlParser: true })) after (() => client.close ()) it ('should abort ordered/unordered bulk operation writes', async () => { const collection = client.db ('Test').collection ('bulk_write_transaction_test') await collection.deleteMany ({}) const session = client.startSession () session.startTransaction ({ readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' }, }) let bulk = undefined // 1. bulk = collection.initializeUnorderedBulkOp ({ session }) bulk.insert ({ 'answer': 42 }) await bulk.execute () // 2. bulk = collection.initializeOrderedBulkOp ({ session }) bulk.insert ({ 'answer': 43 }) await bulk.execute () await session.abortTransaction () await session.endSession () const documents = await collection.find ().toArray () expect (documents).to.be.lengthOf (0, 'bulk operation writes were made outside of transaction') return }) })