An application needs to update a document in the 'orders' collection. If the document matching the query does not exist, a new document must be inserted using fields from both the query and the update document. Which parameter accomplishes this behavior?
Trap 1: { multi: true }
The multi parameter determines whether update operations modify all matching documents or only the first one encountered. It does not control document creation behavior when a filter fails to match any existing records in the target collection.
Trap 2: { upsert: true }
Enabling the upsert flag allows the operation to create a new document when no existing document matches the specified query criteria, utilizing both the query filter and the update document components during the insertion process.
Trap 3: { returnNewDocument: true }
This option modifies the return behavior of findOneAndUpdate operations to supply the updated document rather than the original pre-modification version. It has no impact on whether a missing document is inserted into the collection.
- A
{ multi: true }
Why it fails: The multi parameter determines whether update operations modify all matching documents or only the first one encountered. It does not control document creation behavior when a filter fails to match any existing records in the target collection.
- B
{ upsert: true }
Why it fails: Enabling the upsert flag allows the operation to create a new document when no existing document matches the specified query criteria, utilizing both the query filter and the update document components during the insertion process.
- C
{ upsert: true } is used alongside the query and update parameters to insert a new document when no match is found.
Setting the upsert option to true ensures that if the query criteria match zero documents, MongoDB constructs a new document using fields from the query filter combined with update operators. This prevents duplicate insert attempts in concurrent applications.
- D
{ returnNewDocument: true }
Why it fails: This option modifies the return behavior of findOneAndUpdate operations to supply the updated document rather than the original pre-modification version. It has no impact on whether a missing document is inserted into the collection.