Challenge - 5 Problems
Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
When are transactions necessary in MongoDB?
Which scenario below best describes when you should use transactions in MongoDB?
Attempts:
2 left
💡 Hint
Think about when you need all changes to happen together or not at all.
✗ Incorrect
Transactions are necessary when multiple operations across one or more collections must be atomic, meaning all succeed or all fail together. Simple single-document operations do not require transactions.
🧠 Conceptual
intermediate2:00remaining
When are transactions unnecessary in MongoDB?
Which of the following operations does NOT require a transaction in MongoDB?
Attempts:
2 left
💡 Hint
Think about operations that affect only one document.
✗ Incorrect
Single-document operations in MongoDB are atomic by default and do not require explicit transactions.
❓ query_result
advanced2:00remaining
What is the output of this transaction example?
Consider this MongoDB transaction code snippet. What will be the final value of the 'balance' field in the 'accounts' collection for accountId 123 after the transaction commits successfully?
MongoDB
const session = client.startSession(); let finalBalance; await session.withTransaction(async () => { const account = await accounts.findOne({ accountId: 123 }, { session }); await accounts.updateOne({ accountId: 123 }, { $inc: { balance: -100 } }, { session }); finalBalance = account.balance - 100; }); console.log(finalBalance);
Attempts:
2 left
💡 Hint
Look at how finalBalance is calculated inside the transaction.
✗ Incorrect
The transaction reads the original balance, subtracts 100, updates the document, and sets finalBalance accordingly. After commit, finalBalance reflects the updated balance.
📝 Syntax
advanced2:00remaining
Which option correctly starts a MongoDB transaction session?
Select the correct syntax to start a transaction session in MongoDB using the Node.js driver.
Attempts:
2 left
💡 Hint
Check the official MongoDB Node.js driver method names.
✗ Incorrect
The correct way is to call startSession() on the client, then call startTransaction() on the session object.
🔧 Debug
expert3:00remaining
Why does this MongoDB transaction fail to commit?
Given this code snippet, which operation is missing the session option, so it is outside the transaction?
const session = client.startSession();
await session.withTransaction(async () => {
await orders.insertOne({ orderId: 1, item: 'book' }, { session });
await inventory.updateOne({ item: 'book' }, { $inc: { stock: -1 } });
});
session.endSession();
Attempts:
2 left
💡 Hint
Check if all operations inside the transaction use the session.
✗ Incorrect
All operations inside a transaction must include the session option to be part of the transaction. Missing it means the operation executes outside the transaction.