Courseiva
CRUD Operations →mediumMultiple Choice

C100DBA CRUD Operations Practice Question

You are maintaining an inventory collection where each document has a field named `stock` (integer). A nightly job needs to atomically decrement `stock` by 1 for the document with `sku: "A100"` and also insert the current timestamp into a `lastUpdated` field in the same operation, without overwriting any other fields. Which update operation should you use?

⚠ Common exam trap

The trap here is assuming that $set can perform arithmetic like stock - 1, or that replaceOne is suitable for partial updates.

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.updateOne({ sku: "A100" }, { $inc: { stock: -1 }, $currentDate: { lastUpdated: true } })

The $inc operator atomically increments or decrements a field by a specified amount, and $currentDate sets a field to the current date/time. Combining them in a single updateOne call ensures both modifications happen atomically on the matched document without altering other fields. This directly satisfies the need to decrement stock and add a timestamp in one operation.

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.replaceOne({ sku: "A100" }, { stock: 0, lastUpdated: new Date() })

    Why it's wrong here

    replaceOne replaces the entire document with the provided document, which would remove all other fields and set stock to 0 instead of decrementing it. This does not atomically decrement stock by 1, and it would lose any fields not explicitly included in the replacement document, violating the requirement to preserve other fields.

  • ✗

    db.inventory.updateOne({ sku: "A100" }, { $set: { stock: stock - 1, lastUpdated: true } })

    Why it's wrong here

    The $set operator sets the field to the literal value provided; it cannot reference the existing field value in an expression like stock - 1. This would either set stock to a boolean false or produce an error because stock is not a defined variable in the update document. Also, lastUpdated would be set to boolean true, not a date.

  • ✓

    db.inventory.updateOne({ sku: "A100" }, { $inc: { stock: -1 }, $currentDate: { lastUpdated: true } })

    Why this is correct

    This uses updateOne with the $inc operator to decrement stock by 1 and $currentDate to set lastUpdated to the current date/time, all in a single atomic update. Both fields are modified without affecting other existing fields, exactly matching the requirement to atomically decrement and timestamp the document.

  • ✗

    db.inventory.updateMany({ sku: "A100" }, { $inc: { stock: -1 }, $set: { lastUpdated: new Date() } })

    Why it's wrong here

    updateMany would update all documents matching sku: "A100", but the requirement is to update only one document. Even if only one exists, updateMany is not the appropriate method for a single-document atomic update. Also, using new Date() in the update document sets lastUpdated to the time the update command was constructed, not the server time.

About these practice questions

Courseiva writes every C100DBA question from scratch — 222 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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 C100DBA 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 C100DBA exam.