C100DBA practice question 20 of 25

In mongodb how do you update a document partially?

Full set: 130 questions
Answer options
A: $project
B: $update
C: $modify
D: $set Correct answer
Correct Answer:
D. $set
Explanation

The $set operator is the standard field update operator used to perform partial updates on a document. It replaces the value of a specific field with a new value. If the field does not exist in the document, $set adds it. This allows for modifying one or more fields without replacing the entire document, which is efficient and preserves other field data.

Why Incorrect

A. $project is an aggregation pipeline stage used to reshape the structure of documents in a query result, not to update source documents.
B. $update is not a valid MongoDB update operator. The methods are updateOne() or updateMany(), which use operators like $set.
C. $modify is not a valid MongoDB update operator. The findAndModify command is a method, but it uses operators like $set to perform the modification.

References

1. MongoDB Manual v7.0

"Update and Replace Documents"

"Field Update Operators"

$set section. (https://www.mongodb.com/docs/v7.0/reference/operator/update/set/)

2. MongoDB Manual v7.0

"CRUD Operations"

"Update Documents in MongoDB" section. (https://www.mongodb.com/docs/v7.0/crud/update-documents/)

Scroll to Top