C100DEV CRUD Operations Practice Question
A MongoDB application stores inventory documents in the 'inventory' collection with fields 'sku', 'on_hand', and 'reserved'. During a flash sale, many concurrent sessions must decrement 'on_hand' by 1 only when on_hand is greater than 0, and each successful decrement must return the resulting document to the client. Which approach guarantees this behavior?
⚠ Common exam trap
The trap here is assuming updateOne can return the modified document or that a separate find before an update is safe under concurrency.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
db.inventory.findOneAndUpdate({ sku: 'A100', on_hand: { $gt: 0 } }, { $inc: { on_hand: -1 } }, { returnDocument: 'after' })
findOneAndUpdate performs the conditional match, the $inc decrement, and the return of the document in one atomic operation. The on_hand: { $gt: 0 } filter guards against negative inventory under concurrency, and returnDocument: 'after' supplies the resulting state. Separating read and write, or using updateOne or bulkWrite, either introduces races or fails to return the document.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
db.inventory.findOneAndUpdate({ sku: 'A100', on_hand: { $gt: 0 } }, { $inc: { on_hand: -1 } }, { returnDocument: 'after' })
Why this is correct
findOneAndUpdate atomically matches the filter, applies the decrement, and returns the document. With returnDocument: 'after' the client receives the post-decrement state, and the on_hand: { $gt: 0 } filter prevents negative stock. This single atomic operation satisfies both the conditional decrement and the requirement to return the updated document.
- ✗
db.inventory.bulkWrite([{ updateOne: { filter: { sku: 'A100', on_hand: { $gt: 0 } }, update: { $inc: { on_hand: -1 } } } }])
Why it's wrong here
A bulkWrite performs the conditional atomic decrement correctly, but it returns only a summary of matched and modified counts, not the resulting document. The requirement to return the post-update document to the client is unmet. Also, a bulk write of a single operation offers no concurrency advantage over findOneAndUpdate here.
- ✗
db.inventory.find({ sku: 'A100', on_hand: { $gt: 0 } }) followed by db.inventory.updateOne({ sku: 'A100' }, { $inc: { on_hand: -1 } })
Why it's wrong here
Splitting the read and the write creates a race condition. Between the find and the update, another session can decrement on_hand to zero, so the update still runs and drives stock negative. The two operations are not atomic together, and the update does not return the resulting document either, failing both requirements.
- ✗
db.inventory.updateOne({ sku: 'A100', on_hand: { $gt: 0 } }, { $inc: { on_hand: -1 } }, { returnDocument: 'after' })
Why it's wrong here
updateOne returns a result object with matchedCount and modifiedCount but does not return the post-update document. The returnDocument option belongs to findOneAndUpdate, not updateOne, so it is ignored. The decrement itself is atomic, but the requirement to receive the resulting document is not met by this call.
About these practice questions
This C100DEV question is part of Courseiva's 259-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official MongoDB exam blueprint
This C100DEV practice question is part of Courseiva's free MongoDB certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the C100DEV exam.