Challenge - 5 Problems
MongoDB Transaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this MongoDB transaction commit?
Consider the following MongoDB transaction code snippet. What will be the output after the transaction commits successfully?
MongoDB
const session = client.startSession(); let result; try { session.startTransaction(); const coll = client.db('shop').collection('orders'); result = await coll.insertOne({item: 'book', qty: 3}, {session}); await session.commitTransaction(); } catch (e) { await session.abortTransaction(); } finally { await session.endSession(); } return result.insertedId;
Attempts:
2 left
💡 Hint
Think about what insertOne returns when used inside a transaction with a session.
✗ Incorrect
The insertOne method returns a result object containing insertedId when the insert succeeds inside a transaction. The session is correctly passed, so the transaction commits and returns the inserted document's ObjectId.
📝 Syntax
intermediate1:30remaining
Which option correctly starts a MongoDB session and transaction?
Select the code snippet that correctly starts a session and a transaction in MongoDB.
Attempts:
2 left
💡 Hint
Check the official MongoDB Node.js driver method names for sessions and transactions.
✗ Incorrect
The correct methods are startSession() to start a session and startTransaction() to begin a transaction. Other options use incorrect method names.
🔧 Debug
advanced2:30remaining
Why does this MongoDB transaction code throw an error?
Given the code below, why does it throw an error when trying to commit the transaction?
const session = client.startSession();
session.startTransaction();
const coll = client.db('test').collection('users');
await coll.insertOne({name: 'Alice'});
await session.commitTransaction();
session.endSession();
Attempts:
2 left
💡 Hint
Think about how MongoDB knows which operations belong to the transaction.
✗ Incorrect
The insertOne operation must include the session option to be part of the transaction. Without it, the operation runs outside the transaction, causing commitTransaction to fail.
❓ optimization
advanced2:00remaining
How to optimize MongoDB transaction usage for multiple writes?
You want to perform multiple write operations inside a MongoDB transaction efficiently. Which approach is best?
Attempts:
2 left
💡 Hint
Think about minimizing transaction overhead and ensuring atomicity.
✗ Incorrect
Starting one transaction and performing all writes inside it with the session option minimizes overhead and ensures atomicity. Multiple transactions increase overhead and reduce atomicity.
🧠 Conceptual
expert3:00remaining
What happens if a MongoDB transaction exceeds the maximum allowed duration?
In MongoDB, if a transaction runs longer than the configured maximum transaction lifetime, what is the expected behavior?
Attempts:
2 left
💡 Hint
Consider MongoDB's safeguards to prevent long-running transactions.
✗ Incorrect
MongoDB automatically aborts transactions that exceed the maximum lifetime to avoid resource exhaustion and maintain cluster health.