Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Update Documents

On this page

  • Update Documents in a Collection
  • Behavior

Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


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.

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 different _id field value.

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.

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

←  Iterate a Cursor in mongoshUpdates with Aggregation Pipeline →