Complete the code to start a transaction in MongoDB.
const session = client.startSession();
session.[1]();In MongoDB, the correct method to start a transaction on a session is startTransaction().
Complete the code to commit a transaction in MongoDB.
await session.[1]();The correct method to commit a transaction in MongoDB is commitTransaction().
Fix the error in the code to abort a transaction in MongoDB.
await session.[1]();The correct method to abort a transaction in MongoDB is abortTransaction().
Fill both blanks to set transaction options for read concern and write concern.
const transactionOptions = {
readConcern: { level: '[1]' },
writeConcern: { w: '[2]' }
};For transaction isolation, readConcern is often set to snapshot and writeConcern to majority to ensure strong consistency.
Fill all three blanks to run a transaction with a callback function.
await session.withTransaction(async () => {
await collection.[1](doc, { session });
await collection.[2]({ _id: id }, { session });
}, { readConcern: { level: '[3]' } });Inside a transaction callback, you can perform operations like insertOne and deleteOne. The transaction's read concern is set to snapshot for isolation.