0
0
MongoDBquery~20 mins

When transactions are necessary vs unnecessary in MongoDB - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When are transactions necessary in MongoDB?
Which scenario below best describes when you should use transactions in MongoDB?
AUpdating multiple documents in different collections that must all succeed or fail together
BInserting a single document into a collection
CReading a single document from a collection
DCreating an index on a collection
Attempts:
2 left
💡 Hint
Think about when you need all changes to happen together or not at all.
🧠 Conceptual
intermediate
2:00remaining
When are transactions unnecessary in MongoDB?
Which of the following operations does NOT require a transaction in MongoDB?
AUpdating multiple documents in different collections atomically
BInserting a single document into a collection
CUpdating related documents in multiple collections
DPerforming multiple writes that must all succeed or fail together
Attempts:
2 left
💡 Hint
Think about operations that affect only one document.
query_result
advanced
2: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);
AUndefined, because finalBalance is not set
BOriginal balance
COriginal balance minus 100
DOriginal balance plus 100
Attempts:
2 left
💡 Hint
Look at how finalBalance is calculated inside the transaction.
📝 Syntax
advanced
2: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.
Aconst session = client.transaction();
Bconst session = client.startTransaction();
Cconst session = client.beginSession(); session.beginTransaction();
Dconst session = client.startSession(); session.startTransaction();
Attempts:
2 left
💡 Hint
Check the official MongoDB Node.js driver method names.
🔧 Debug
expert
3: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();
AThe updateOne operation is missing the session option, so it is outside the transaction
BThe insertOne operation is missing the session option, so it is outside the transaction
Csession.endSession() must be called before withTransaction
DTransactions are not supported on single-document operations
Attempts:
2 left
💡 Hint
Check if all operations inside the transaction use the session.