D. endSession() must be called before commitTransaction()
Solution
Step 1: Check async calls in transaction
commitTransaction() returns a promise and must be awaited to complete properly.
Step 2: Identify missing await causes error
Without await, the code may proceed before commit finishes, causing errors.
Final Answer:
Missing await before commitTransaction() -> Option C
Quick Check:
Async commit needs await [OK]
Hint: Always await commitTransaction() to avoid errors [OK]
Common Mistakes:
Not awaiting commitTransaction()
Calling startTransaction() multiple times
Calling endSession() too early
5. You want to update two documents atomically in MongoDB. You start a transaction, update the first document, then update the second. If the second update fails, what should you do to ensure no partial changes remain?
hard
A. Manually delete the first update after failure.
B. Commit the transaction to save the first update only.
C. Ignore the failure and continue.
D. Abort the transaction to cancel all changes.
Solution
Step 1: Understand atomicity in transactions
Transactions ensure all-or-nothing changes. If one update fails, the whole transaction should be canceled.
Step 2: Apply abort to cancel partial changes
Aborting the transaction cancels all changes, preventing partial updates.
Final Answer:
Abort the transaction to cancel all changes. -> Option D
Quick Check:
Fail in transaction = abort all changes [OK]
Hint: Abort transaction to avoid partial updates on failure [OK]
Common Mistakes:
Committing partial updates
Trying to manually fix partial changes
Ignoring failure and continuing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.