0
0
MongoDBquery~20 mins

Multi-document transactions 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 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?
ABoth collections have the new documents inserted by the transaction.
BOnly the 'users' collection has the new document; 'orders' is empty.
CNeither collection has the new documents because transactions are not supported.
DOnly the 'orders' collection has the new document; 'users' is empty.
Attempts:
2 left
💡 Hint
Think about what commitTransaction() does after successful operations inside a transaction.
🧠 Conceptual
intermediate
1:30remaining
Which statement about MongoDB multi-document transactions is true?
Select the correct statement about multi-document transactions in MongoDB.
ATransactions automatically retry on network errors without developer intervention.
BTransactions lock the entire database during their execution.
CTransactions can span multiple collections and databases within the same replica set.
DTransactions are supported only on standalone MongoDB servers.
Attempts:
2 left
💡 Hint
Think about the scope and environment where transactions work in MongoDB.
🔧 Debug
advanced
2: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();
}
ATransactions cannot update documents in the same collection twice.
BThe commitTransaction() method was called before any operations.
CThe session was not properly started before the transaction.
DThe transaction tries to update the same document twice causing a write conflict.
Attempts:
2 left
💡 Hint
Consider what happens when two operations inside a transaction modify the same document.
📝 Syntax
advanced
1: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.
A
const session = client.startSession();
session.startTransaction();
Bconst session = client.startTransaction();
Cclient.startSession().startTransaction();
Dconst session = client.transaction(); session.begin();
Attempts:
2 left
💡 Hint
Remember the two-step process: start a session, then start a transaction on that session.
optimization
expert
3: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?
AIncrease the transaction timeout to allow more time for operations to complete.
BKeep the transaction as short as possible by minimizing the number of operations inside it.
CUse multiple nested transactions to isolate updates per collection.
DDisable journaling to speed up writes during the transaction.
Attempts:
2 left
💡 Hint
Think about how long locks are held during a transaction.