Courseiva
CRUD Operations →easyMultiple Choice

C100DBA CRUD Operations Practice Question

A developer needs to insert a document into the inventory collection and wants the operation to fail with a duplicate key error if a document with the same value for the sku field already exists. The sku field has a unique index. Which insert method and option combination achieves this with the least code?

⚠ Common exam trap

The trap here is thinking that an insert option is required to enforce uniqueness, when the unique index itself already causes the duplicate key error.

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.insertOne( { sku: "A100", qty: 5 } )

A unique index on sku is what enforces the duplicate constraint. A plain insertOne is sufficient: if the sku already exists, the server returns a duplicate key error. The ordered option is only relevant for multi-document inserts, upsert would update instead of fail, and bypassDocumentValidation only affects schema validation, not unique indexes.

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.insertOne( { sku: "A100", qty: 5 } )

    Why this is correct

    insertOne inserts the document and, because sku has a unique index, a duplicate value causes a duplicate key error to be thrown. This is the simplest correct approach: no extra option is needed. The unique index itself enforces the constraint, so the operation fails as required when a duplicate sku is present.

  • ✗

    db.inventory.updateOne( { sku: "A100" }, { $set: { qty: 5 } }, { upsert: true } )

    Why it's wrong here

    This update with upsert inserts only when no document matches the filter. If a document with sku "A100" already exists, it updates that document instead of failing, which is the opposite of the required behavior. It does not produce a duplicate key error and therefore does not satisfy the scenario.

  • ✗

    db.inventory.insertMany( [ { sku: "A100", qty: 5 } ], { bypassDocumentValidation: true } )

    Why it's wrong here

    bypassDocumentValidation skips schema validation rules, not unique index constraints. A duplicate sku would still trigger a duplicate key error, but the option is irrelevant to the requirement and adds unnecessary complexity. It also does not reduce code or improve correctness compared to a plain insertOne.

  • ✗

    db.inventory.insertOne( { sku: "A100", qty: 5 }, { ordered: false } )

    Why it's wrong here

    The ordered option controls whether the server stops at the first error when inserting multiple documents. For a single insertOne it has no effect on duplicate detection. The operation would still throw a duplicate key error on a duplicate sku, but the option is unnecessary and does not change the behavior described.

About these practice questions

This C100DBA question is part of Courseiva's 222-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 →

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.