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 multi-document transaction commit?
Consider a MongoDB transaction that inserts two documents into two different collections and then commits. What will be the state of the collections after the commit?
MongoDB
const session = client.startSession(); session.startTransaction(); try { await db.collection('users').insertOne({ _id: 1, name: 'Alice' }, { session }); await db.collection('orders').insertOne({ _id: 101, item: 'Book' }, { session }); await session.commitTransaction(); } catch (e) { await session.abortTransaction(); } finally { session.endSession(); } // What documents exist now in 'users' and 'orders' collections?
Attempts:
2 left
💡 Hint
Think about what commitTransaction() does after successful operations inside a transaction.
✗ Incorrect
When a multi-document transaction commits successfully, all operations inside it become visible atomically. So both documents are inserted.
🧠 Conceptual
intermediate1:30remaining
Which statement about MongoDB multi-document transactions is true?
Select the correct statement about multi-document transactions in MongoDB.
Attempts:
2 left
💡 Hint
Think about the scope and environment where transactions work in MongoDB.
✗ Incorrect
MongoDB supports multi-document transactions across multiple collections and databases but only within a replica set or sharded cluster.
🔧 Debug
advanced2:30remaining
Why does this transaction abort with an error?
Examine the following MongoDB transaction code snippet. Why does it abort with a write conflict error?
MongoDB
const session = client.startSession(); session.startTransaction(); try { await db.collection('inventory').updateOne({ _id: 1 }, { $inc: { qty: -1 } }, { session }); await db.collection('inventory').updateOne({ _id: 1 }, { $inc: { qty: -1 } }, { session }); await session.commitTransaction(); } catch (e) { await session.abortTransaction(); throw e; } finally { session.endSession(); }
Attempts:
2 left
💡 Hint
Consider what happens when two operations inside a transaction modify the same document.
✗ Incorrect
Updating the same document twice in a transaction can cause a write conflict if the document version changes between operations.
📝 Syntax
advanced1:30remaining
Which option correctly starts a multi-document transaction in MongoDB?
Choose the correct syntax to start a multi-document transaction using the MongoDB Node.js driver.
Attempts:
2 left
💡 Hint
Remember the two-step process: start a session, then start a transaction on that session.
✗ Incorrect
You must first start a session, then call startTransaction() on that session object.
❓ optimization
expert3:00remaining
How to optimize a multi-document transaction to reduce lock contention?
You have a MongoDB multi-document transaction that updates multiple documents across collections. Which approach best reduces lock contention and improves performance?
Attempts:
2 left
💡 Hint
Think about how long locks are held during a transaction.
✗ Incorrect
Shorter transactions hold locks for less time, reducing contention and improving concurrency.