-
Type: Task
-
Resolution: Gone away
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Docs
-
None
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.
- is related to
-
NODE-1843 Bulk operation executes outside provided session transaction
- Closed