0
0
MongoDBquery~20 mins

Session and transaction syntax in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Transaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
AUndefined because session is not passed
Bnull
CThrows a SyntaxError
DObjectId of the inserted document
Attempts:
2 left
💡 Hint
Think about what insertOne returns when used inside a transaction with a session.
📝 Syntax
intermediate
1:30remaining
Which option correctly starts a MongoDB session and transaction?
Select the code snippet that correctly starts a session and a transaction in MongoDB.
Aconst session = client.startSession(); session.begin();
Bconst session = client.startSession(); session.startTransaction();
Cconst session = client.sessionStart(); session.beginTransaction();
Dconst session = client.newSession(); session.startTransaction();
Attempts:
2 left
💡 Hint
Check the official MongoDB Node.js driver method names for sessions and transactions.
🔧 Debug
advanced
2: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();
AThe insertOne operation is missing the session option, so it is outside the transaction
BcommitTransaction must be called before startTransaction
Csession.endSession() must be awaited
DTransactions are not supported on the 'test' database
Attempts:
2 left
💡 Hint
Think about how MongoDB knows which operations belong to the transaction.
optimization
advanced
2: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?
AStart a session and transaction once, perform all writes with the session option, then commit once
BStart and commit a transaction for each write operation separately
CPerform writes without a transaction to avoid overhead
DUse multiple sessions, one per write, and commit each separately
Attempts:
2 left
💡 Hint
Think about minimizing transaction overhead and ensuring atomicity.
🧠 Conceptual
expert
3: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?
AThe transaction pauses until manually resumed
BThe transaction commits automatically regardless of duration
CThe transaction is automatically aborted by the server
DThe transaction continues indefinitely without interruption
Attempts:
2 left
💡 Hint
Consider MongoDB's safeguards to prevent long-running transactions.