Complete the code to start a transaction in MongoDB.
const session = client.startSession();
session.[1]();In MongoDB, startTransaction() is the correct method to begin a transaction session.
Complete the code to commit a transaction in MongoDB.
await session.[1]();The method commitTransaction() commits the changes made in the transaction.
Fix the error in the code to abort a transaction properly.
await session.[1]();The correct method to abort a transaction in MongoDB is abortTransaction().
Fill both blanks to correctly use a transaction with a session in MongoDB.
const session = client.startSession(); try { session.[1](); await collection.insertOne(doc, { session: [2] }); await session.commitTransaction(); } finally { await session.endSession(); }
We start the transaction with startTransaction() and pass the session object to the operation.
Fill all three blanks to handle a transaction with error handling in MongoDB.
const session = client.startSession(); try { session.[1](); await collection.updateOne(filter, update, { session: [2] }); await session.[3](); } catch (error) { await session.abortTransaction(); } finally { await session.endSession(); }
We start the transaction with startTransaction(), pass the session to the update operation, and commit with commitTransaction().