Update Documents
On this page
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.
Update Documents in a Collection
Note
Starting in MongoDB 4.2, MongoDB can accept an aggregation pipeline to specify the modifications to make instead of an update document. See the method reference page for details.
Update a Single Document
Behavior
Atomicity
All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.
_id
Field
Once set, you cannot update the value of the _id
field nor can you replace an existing document with a replacement document that has a different _id
field value.
Field Order
For write operations, MongoDB preserves the order of the document fields except for the following cases:
-
The
_id
field is always the first field in the document. -
Updates that include
renaming
of field names may result in the reordering of fields in the document.
Write Acknowledgement
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.