C100DBA · Question #91
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. Passing { upsert: true } as the third parameter to MongoDB's update() command tells the database engine to insert a new document when no existing document matches the query - this is built-in MongoDB behavior, no extra tooling required. Option A is wrong because upsert is not a…
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
- B{update: true, insert: true} as the third parameter of update command
- CThis has to be handled in application code (Node.js, PHP, JAVA, C#, etc.) and cannot be handled in
- DSpecify {upsert : true } as the third parameter of update command
How the community answered
(30 responses)- B7% (2)
- C3% (1)
- D90% (27)
Explanation
Passing { upsert: true } as the third parameter to MongoDB's update() command tells the database engine to insert a new document when no existing document matches the query - this is built-in MongoDB behavior, no extra tooling required. Option A is wrong because upsert is not a separate command; it's an option flag on the existing update() command. Option B is wrong because MongoDB does not recognize { update: true, insert: true } as valid syntax - that object is simply made up. Option C is wrong because this logic is handled natively by the database driver, not by application-layer code. Memory tip: Think of "upsert" as a portmanteau of update + insert - when you set { upsert: true }, MongoDB either updates what it finds or inserts what it doesn't.
Topics
Community Discussion
No community discussion yet for this question.