Complete the code to start a transaction session in MongoDB.
const session = client.startSession();
await session.[1]();The startTransaction() method begins a transaction in the session.
Complete the code to commit the transaction after operations.
await session.[1]();The commitTransaction() method saves all changes made during the transaction.
Fix the error in the code to abort the transaction on failure.
try { await session.startTransaction(); // operations await session.commitTransaction(); } catch (error) { await session.[1](); }
On error, abortTransaction() cancels all changes made in the transaction.
Fill both blanks to run operations inside a transaction session.
const session = client.startSession(); try { await session.[1](); await collection.[2](doc, { session }); await session.commitTransaction(); } finally { await session.endSession(); }
Start the transaction with startTransaction() and perform the insert with insertOne() inside the session.
Fill all three blanks to update two documents atomically in a transaction.
const session = client.startSession(); try { await session.[1](); await collection1.[2]({ _id: 1 }, { $set: { status: 'done' } }, { session }); await collection2.[3]({ _id: 2 }, { $inc: { count: 1 } }, { session }); await session.commitTransaction(); } catch (e) { await session.abortTransaction(); } finally { await session.endSession(); }
Start the transaction, then update documents in both collections using updateOne() inside the session.
