For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Update Documents

In MongoDB, an Update operation consists of adding, removing, or modifying fields in one or more documents. This page provides examples.

You can update documents in MongoDB using the following methods:


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


The following examples use the movies collection from the sample_mflix database. To learn how to load the sample dataset into your deployment, see Load Sample Data.

To update a document, MongoDB provides Field Update Operators, such as $set, to modify field values.

To use the update operators, pass to the update methods an update document of the form:

{ <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ...}

Some update operators, such as $set, will create the field if the field does not exist. See the individual update operator reference for details.

The following example uses the db.collection.updateOne() method on the movies collection to update the first document where title equals "The Godfather":

db.movies.updateOne( { title: "The Godfather" }, { $set: { rated: "PG", "tomatoes.viewer.rating": 4.5 }, $currentDate: { lastupdated: true } })

The update operation:

  • uses the $set operator to update the value of the size.uom field to "cm" and the value of the status field to "P",

  • uses the $currentDate operator to update the value of the lastModified field to the current date. If lastModified field does not exist, $currentDate will create the field. See $currentDate for details.

The following example uses the db.collection.updateMany() method on the movies collection to update all documents where num_mflix_comments is greater than 100:

db.movies.updateMany( { "num_mflix_comments": { $gt: 100 } }, { $set: { popular: true }, $currentDate: { lastupdated: true } })

To replace the entire content of a document except for the _id field, pass an entirely new document as the second argument to db.collection.replaceOne().

When replacing a document, the replacement document must consist of only field/value pairs. The replacement document cannot include update operators expressions.

The replacement document can have different fields from the original document. In the replacement document, you can omit the _id field since the _id field is immutable. However, if you do include the _id field, it must have the same value as the current value.

The following example replaces the first document from the movies collection where title: "The Godfather":

db.movies.replaceOne( { title: "The Godfather" }, { title: "The Godfather", plot: "Updated plot summary", year: 1972, rated: "R", runtime: 175 })

Note

You can update only one document at a time in the MongoDB Atlas UI. To update multiple documents or replace an entire document, connect to your Atlas deployment from mongosh or a MongoDB driver and follow the examples on this page for your preferred method.

The example in this section uses the sample supplies dataset. To learn how to load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.

To update a document in MongoDB Atlas, follow these steps:

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Clusters under the Database heading.

    The Clusters page displays.

2
  1. For the cluster that contains the sample data, click Browse Collections.

  2. In the left navigation pane, select the sample_supplies database.

  3. Select the sales collection.

3

You can specify a query filter document in the Filter field. A query filter document uses query operators to specify search conditions.

Copy the following query filter document into the Filter search bar and click Apply:

{ saleDate: { $gte: { $date: "2016-01-01T00:00-00:00" }, $lte: { $date: "2016-01-02T00:00-00:00" } } }

This query filter returns all documents in the sample_supplies.sales collection where saleDate is on or between January 1 and 2, 2016 UTC time.

4

To edit a document displayed in the query results, hover over the document and click on the pencil icon. In the document editor, you can:

  • Add a new field.

  • Delete an existing field.

  • Edit a field's name, value, or type.

  • Revert a specific change.

For detailed instructions, see Create, View, Update, and Delete Documents.

5

To confirm and save your changes, click the Update button.

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.

Use the updateMany() method only for idempotent operations.

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.

If updateOne(), updateMany(), or replaceOne() includes upsert : true and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.

For details on the new document created, see the individual reference pages for the methods.

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

Select your language
Rate this page