MongoDB Associate Database Administrator(Based on MongoDB 6.0 and 7.0+) C100DBA Question # 15 Topic 2 Discussion
C100DBA Exam Topic 2 Question 15 Discussion:
Question #: 15
Topic #: 2
What operation updates the value of the unitsInStock field to 25 for a document with a " laptop " product, and inserts a new document if it does not exist?
An upsert combines update behavior with conditional insertion. updateOne() first searches for a document matching { product: " laptop " }. If one exists, $set changes unitsInStock to 25. If none exists and the options document contains { upsert: true }, MongoDB inserts a new document based on the equality portions of the filter together with the update result. Option A uses exactly this pattern. Options B and C incorrectly use insertOne(), which inserts a supplied document and does not accept an update-operator document plus upsert semantics in this form. Option D uses the correct method and update structure but specifies True with a capital T. Mongosh uses JavaScript Boolean syntax, so the literal must be lowercase true. Upserts are useful when an application needs to ensure that a logical entity exists after an operation, but DBAs should consider a unique index on the identifying field when duplicate creation by concurrent requests must be prevented. Given the choices, updateOne(..., {$set:...}, {upsert:true}) is the valid operation, so A is correct.
Study Guide reference/topic:MongoDB Architecture and Core Operations - updateOne(), $set, upserts, and Boolean syntax.
===========
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit