In MongoDB, how does increasing the size of a transaction (number of operations inside it) generally affect performance?
Think about how holding locks longer affects other operations.
Larger transactions hold locks for longer periods, which can block other operations and increase resource consumption, leading to degraded performance.
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?
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;
Consider when start and end are recorded relative to the transaction.
The duration variable measures the elapsed time from before starting the transaction to after committing or aborting it, thus capturing the total transaction time.
Which of the following MongoDB transaction code snippets contains a syntax error that prevents it from running?
const session = client.startSession(); session.startTransaction(); try { await collection.insertOne({ name: 'Alice' }, { session }); await session.commitTransaction(); } catch (error) { await session.abortTransaction(); } finally { session.endSession(); }
Check if await is used inside an async function.
Using await outside an async function causes a syntax error in JavaScript.
Which strategy best reduces transaction contention and improves performance in MongoDB?
Think about how lock duration affects other operations.
Shorter transactions that modify fewer documents hold locks for less time, reducing contention and improving overall performance.
A MongoDB transaction frequently aborts with a timeout error. Which of the following is the most likely cause?
Consider what causes transactions to exceed timeout limits.
Long-running transactions that hold locks too long can cause timeout errors because MongoDB aborts transactions that exceed configured time limits to avoid blocking other operations.