0
0
MongoDBquery~20 mins

Transaction performance considerations in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transaction Performance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Impact of Transaction Size on Performance

In MongoDB, how does increasing the size of a transaction (number of operations inside it) generally affect performance?

APerformance degrades because larger transactions hold locks longer and consume more resources.
BPerformance improves because more operations are grouped together reducing overhead.
CPerformance remains the same regardless of transaction size.
DPerformance improves only if the transaction contains write operations.
Attempts:
2 left
💡 Hint

Think about how holding locks longer affects other operations.

query_result
intermediate
2:00remaining
Transaction Duration Measurement Query

Given a MongoDB collection orders with a field status, which query snippet correctly measures the total time taken by a transaction that updates multiple documents?

MongoDB
const session = client.startSession();
const start = Date.now();
session.startTransaction();
try {
  await orders.updateMany({ status: 'pending' }, { $set: { status: 'processed' } }, { session });
  await orders.updateMany({ status: 'processed' }, { $set: { status: 'completed' } }, { session });
  await session.commitTransaction();
} catch (e) {
  await session.abortTransaction();
}
const end = Date.now();
const duration = end - start;
AThe variable <code>duration</code> measures only the commitTransaction time.
BThe variable <code>duration</code> holds the total transaction time in milliseconds.
CThe variable <code>duration</code> will always be zero because transactions are asynchronous.
DThe variable <code>duration</code> holds the time only for the first updateMany operation.
Attempts:
2 left
💡 Hint

Consider when start and end are recorded relative to the transaction.

📝 Syntax
advanced
2:00remaining
Identify the Syntax Error in Transaction Usage

Which of the following MongoDB transaction code snippets contains a syntax error that prevents it from running?

MongoDB
const session = client.startSession();
session.startTransaction();
try {
  await collection.insertOne({ name: 'Alice' }, { session });
  await session.commitTransaction();
} catch (error) {
  await session.abortTransaction();
} finally {
  session.endSession();
}
ACalling <code>session.endSession()</code> inside finally block is invalid syntax.
BMissing async keyword before the function containing await calls.
CUsing <code>await</code> without <code>async</code> causes a syntax error.
DThe <code>startTransaction()</code> method requires a callback function.
Attempts:
2 left
💡 Hint

Check if await is used inside an async function.

optimization
advanced
2:00remaining
Optimizing Transaction Performance by Reducing Contention

Which strategy best reduces transaction contention and improves performance in MongoDB?

AUse shorter transactions that touch fewer documents to reduce lock time.
BIncrease transaction size to batch more operations together.
CUse transactions only for read operations to avoid write locks.
DDisable journaling to speed up commit times.
Attempts:
2 left
💡 Hint

Think about how lock duration affects other operations.

🔧 Debug
expert
2:00remaining
Diagnosing a Transaction Timeout Issue

A MongoDB transaction frequently aborts with a timeout error. Which of the following is the most likely cause?

AThe client session was not started before the transaction.
BThe transaction uses an unsupported write concern level.
CThe transaction includes too many read operations causing network delays.
DThe transaction holds locks for too long due to large or slow operations.
Attempts:
2 left
💡 Hint

Consider what causes transactions to exceed timeout limits.