If upsert
is true and no document matches the query criteria, update()
inserts a single document. The update creates the new document with either:
- The fields and values of the
update
parameter if the update
parameter is a replacement document (i.e., contains only field and value pairs). If neither the query
nor the update
document specifies an _id
field, MongoDB adds the _id
field with an ObjectId
value. - The fields and values of both the
query
and update
parameters if the update
parameter contains update operator expressions (such as $inc
, $set
, $min
, $max
). The update creates a base document from the equality clauses in the parameter, and then applies the update expressions from the update
parameter. Comparison operations from the query
will not be included in the new document.
Also if we want to update multiple documents we can pass multi: true
as another option.