➤ 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¶
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¶
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.
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
_id field value.
For write operations, MongoDB preserves the order of the document fields except for the following cases:
_idfield is always the first field in the document.
- Updates that include
renamingof field names may result in the reordering of fields in the document.
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.