nerdexam
MongoDB

C100DBA · Question #102

Which option can be used with update command so that a new document gets created if no matching document is found based on the query condition?

The correct answer is D. Specify {upsert : true } as the third parameter of update command. Option D is correct because MongoDB's update() command accepts an options object as its third parameter, and setting { upsert: true } tells MongoDB to insert a new document if no existing document matches the query - combining "update" and "insert" into one atomic operation…

MongoDB Fundamentals

Question

Which option can be used with update command so that a new document gets created if no matching document is found based on the query condition?

Options

  • Aupsert command instead of update command
  • BThis has to be handled in application code (Node.js, PHP, JAVA, C#, etc.) and cannot be handled in
  • C{update: true, insert: true} as the third parameter of update command
  • DSpecify {upsert : true } as the third parameter of update command

How the community answered

(20 responses)
  • A
    5% (1)
  • B
    10% (2)
  • D
    85% (17)

Explanation

Option D is correct because MongoDB's update() command accepts an options object as its third parameter, and setting { upsert: true } tells MongoDB to insert a new document if no existing document matches the query - combining "update" and "insert" into one atomic operation.

Why the distractors are wrong:

  • A is wrong because upsert is not a separate command - it's an option within the update command, not a replacement for it.
  • B is wrong because MongoDB handles this natively at the database level; no application-side logic is needed.
  • C is wrong because { update: true, insert: true } is not valid MongoDB syntax - the correct key is upsert, not separate update/insert flags.

Memory tip: Think of "upsert" as a portmanteau of UPdate + inSERT - it does both in one step. The option lives in the third parameter (after the query and update document), just like other behavioral flags such as multi.

Topics

#upsert#update options#insert if not exists

Community Discussion

No community discussion yet for this question.

Full C100DBA Practice