Definition
updateThe
updatecommand modifies documents in a collection. A singleupdatecommand can contain multiple update statements.Tip
In
mongosh, this command can also be run through theupdateOne(),updateMany(),replaceOne(),findOneAndReplace(), andfindOneAndUpdate()helper methods.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
Compatibility
This command is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
Changed in version 8.0.
The command has the following syntax:
db.runCommand( { update: <collection>, updates: [ { q: <query>, u: <document or pipeline>, c: <document>, // Added in MongoDB 5.0 upsert: <boolean>, multi: <boolean>, collation: <document>, arrayFilters: <array>, hint: <document|string>, sort: <document> }, ... ], ordered: <boolean>, maxTimeMS: <integer>, writeConcern: { <write concern> }, bypassDocumentValidation: <boolean>, comment: <any>, let: <document> // Added in MongoDB 5.0 } )
Command Fields
The command takes the following fields:
Field | Type | Description | |||||
|---|---|---|---|---|---|---|---|
| string | The name of the target collection. | |||||
| array | An array of one or more update statements to perform on the named collection. For details of the update statements, see Update Statements. | |||||
| boolean | Optional. If | |||||
| non-negative integer | Optional. Specifies a time limit in milliseconds.
If you do not specify a value for MongoDB terminates operations that exceed their allotted time limit
using the same mechanism as | |||||
| document | Optional. A document expressing the write concern
of the Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern. | |||||
| boolean | Optional. Enables | |||||
| any | Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
A comment can be any valid BSON type (string, integer, object, array, etc). | |||||
document | Optional. Specifies a document with a list of variables. This allows you to improve command readability by separating the variables from the query text. The document syntax is: The variable is set to the value returned by the expression, and cannot be changed afterwards. To access the value of a variable in the command, use the double
dollar sign prefix ( For a complete example, see Use Variables in New in version 5.0. | ||||||
| document | Optional. Orders the documents before the update is applied. If the sort argument is not a document, the operation errors. MongoDB does not store documents in a collection in a particular order. When sorting on a field which contains duplicate values, documents containing those values may be returned in any order. The If the field specified in the sort criteria does not exist in two documents, then the value on which they are sorted is the same. The two documents may be returned in any order. If consistent sort order is desired, include at least one field in your
sort that contains unique values. The easiest way to guarantee this is
to include the For more information, see Sort Consistency. New in version 8.0. You cannot use For a |
Update Statements
Each element of the updates array is an update statement document.
Each document contains the following fields:
Field | Type | Description | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
document | The query that matches documents to update. Use the same query
selectors as used in the | |||||||||||||||||||
document or pipeline | The modifications to apply. The value can be either:
For details, see Behavior. | |||||||||||||||||||
document | Optional. You can specify IMPORTANT: Starting in MongoDB 8.2, if you specify Specifies a document with a list of variables. This allows you to improve command readability by separating the variables from the query text. The document syntax is: The variable is set to the value returned by the expression, and cannot be changed afterwards. To access the value of a variable in the command, use the double
dollar sign prefix ( To use a variable to filter results, you must access the variable
within the For a complete example using New in version 5.0. | |||||||||||||||||||
boolean | Optional. When
If both To avoid multiple upserts, ensure that the
Defaults to | |||||||||||||||||||
| boolean | Optional. If When updating multiple documents, if a single document fails to update, further documents are not updated. See multi-update failures for more details on this behavior. | ||||||||||||||||||
| document | Optional. Specifies the collation to use for the operation. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. The collation option has the following syntax: When specifying collation, the If the collation is unspecified but the collection has a
default collation (see If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons. You cannot specify multiple collations for an operation. For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort. | ||||||||||||||||||
| array | Optional. An array of filter documents that determines which array elements to modify for an update operation on an array field. In the update document, use the The You can include the same identifier multiple times in the update
document; however, for each distinct identifier ( However, you can specify compound conditions on the same identifier in a single filter document, such as in the following examples: For examples, see Specify | ||||||||||||||||||
Document or string | Optional. A document or string that specifies the index to use to support the query predicate. The option can take an index specification document or the index name string. If you specify an index that does not exist, the operation errors. For an example, see Specify |
Returns
The command returns a document that contains the status of the operation. For example:
{ "ok" : 1, "nModified" : 0, "n" : 1, "upserted" : [ { "index" : 0, "_id" : ObjectId("52ccb2118908ccd753d65882") } ] }
For details of the output fields, see Output.
Access Control
On deployments running with authorization, the
user must have access that includes the following privileges:
updateaction on the specified collection(s).findaction on the specified collection(s).insertaction on the specified collection(s).
The built-in role readWrite provides the required
privileges.
Behavior
Limitations
If you set multi: true, use the update command only for
idempotent operations.
Update with an Update Operator Expressions Document
The update statement field u can accept a document that only contains update operator expressions. For example:
updates: [ { q: <query>, u: { $set: { status: "D" }, $inc: { quantity: 2 } }, ... }, ... ]
Then, the update command updates only the corresponding
fields in the document.
Update with a Replacement Document
The update statement field u field can accept
a replacement document, i.e. the document contains only
field:value expressions. For example:
updates: [ { q: <query>, u: { status: "D", quantity: 4 }, ... }, ... ]
Then the update command replaces the matching document
with the update document. The update command can only
replace a single matching document; i.e. the multi field cannot
be true. The update command does not replace the
_id value.
Multi-Update Failures
If a single document fails to update in an update command with the
multi parameter set to true, no further documents
update as part of that command.
For example, the sample_mflix.movies collection contains movies with
imdb.rating fields. Create a document validator on the movies collection with a rule
that the imdb.rating value must be less than or equal to 10:
db.runCommand( { update: "movies", updates: [ { q: { year: { $gte: 2000, $lte: 2005 }, "imdb.rating": { $type: "number" } }, u: { $inc: { "imdb.rating": 1 } }, multi: true } ] } )
If any movie already has a rating of 10, incrementing it would violate
the validator rule (rating > 10). When this happens, the update stops and
no further documents are updated, even if thousands of documents matched
the query.
Note
If a subset of matched documents are updated, such as when an update
would cause some documents to fail schema validation, the value of
nModified returned by the update command might not be accurate.
Update with an Aggregation Pipeline
The update statement field u field can accept an
aggregation pipeline
[ <stage1>, <stage2>, ... ] that specifies the modifications to perform.
The pipeline can consist of the following stages:
$addFieldsand its alias$set$replaceRootand its alias$replaceWith
Using the aggregation pipeline allows for a more expressive update statement, such as expressing conditional updates based on current field values or updating one field using the value of another field(s).
For example:
updates: [ { q: <query>, u: [ { $set: { status: "Modified", comments: [ "$misc1", "$misc2" ] } }, { $unset: [ "misc1", "misc2" ] } ], ... }, ... ]
Note
For examples, see Update with Aggregation Pipeline.
Upsert with Unique Index
Upserts can create duplicate documents, unless there is a unique index to prevent duplicates.
Consider an example where no document with the name Andy exists
and multiple clients issue the following command at roughly the same
time:
db.runCommand( { update: "people", updates: [ { q: { name: "Andy" }, u: { $inc: { score: 1 } }, multi: true, upsert: true } ] } )
If all update operations finish the query phase before any
client successfully inserts data, and there is no unique index on
the name field, each update operation may result in an
insert, creating multiple documents with name: Andy.
A unique index on the name field ensures that only one document
is created. With a unique index in place, the multiple update
operations now exhibit the following behavior:
Exactly one
updateoperation will successfully insert a new document.Other
updateoperations either update the newly-inserted document or fail due to a unique key collision.In order for other
updateoperations to update the newly-inserted document, all of the following conditions must be met:The target collection has a unique index that would cause a duplicate key error.
The update operation is not
updateManyormultiisfalse.The update match condition is either:
A single equality predicate. For example
{ "fieldA" : "valueA" }A logical AND of equality predicates. For example
{ "fieldA" : "valueA", "fieldB" : "valueB" }
The fields in the equality predicate match the fields in the unique index key pattern.
The update operation does not modify any fields in the unique index key pattern.
The following table shows examples of upsert operations that,
when a key collision occurs, either result in an update or fail.
Unique Index Key Pattern | Update Operation | Result | ||||||
|---|---|---|---|---|---|---|---|---|
| | The | ||||||
| | The operation fails because it modifies the field in the
unique index key pattern ( | ||||||
| | The operation fails because the equality predicate fields
( |
Limits
For each update element in the updates array, the sum of the query
and the update sizes (i.e. q and u ) must be less than or equal
to the maximum BSON document size.
The total number of update statements in the updates array must be
less than or equal to the maximum bulk size.
Schema Validation
The update command adds support for the
bypassDocumentValidation option, which lets you bypass
schema validation when
inserting or updating documents in a collection with validation
rules.
Sharded Collections
upsert on a Sharded Collection
To use update with multi: false on a sharded
collection,
If you do not specify upsert: true, the filter q must either include an equality match on the
_idfield or target a single shard (such as by including the shard key).If you specify upsert: true, the filter q must include an equality match on the shard key.
However, documents in a sharded collection can be missing the shard key fields. To target a document that is missing the shard key, you can use the
nullequality match in conjunction with another filter condition (such as on the_idfield). For example:{ _id: <value>, <shardkeyfield>: null } // _id of the document missing shard key
Replace Document
When replacing a document, update attempts to target a shard,
first by using the query filter. If the operation cannot target a single shard
by the query filter, it then attempts to target by the replacement document.
Shard Key Modification
You can update a document's shard key value unless the shard key field is the
immutable _id field.
To modify the existing shard key value with
update:
You must run on a
mongos. Do not issue the operation directly on the shard.You must run either in a transaction or as a retryable write.
You must specify
multi: false.You must include an equality query filter on the full shard key.
Tip
Since a missing key value is returned as part of a null equality
match, to avoid updating a null-valued key, include additional
query conditions (such as on the _id field) as appropriate.
See also upsert on a Sharded Collection.
Missing Shard Key
Documents in a sharded collection can be
missing the shard key fields. To use
update to set the document's
missing shard key, you must run on a
mongos. Do not issue the operation directly on
the shard.
In addition, the following requirements also apply:
Task | Requirements |
|---|---|
To set to |
|
To set to a non- |
|
Tip
Since a missing key value is returned as part of a null equality
match, to avoid updating a null-valued key, include additional
query conditions (such as on the _id field) as appropriate.
See also:
Transactions
update can be used inside distributed transactions.
Important
In most cases, a distributed transaction incurs a greater performance cost over single document writes, and the availability of distributed transactions should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for distributed transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
Upsert within Transactions
You can create collections and indexes inside a distributed transaction if the transaction is not a cross-shard write transaction.
update with upsert: true can be run on an existing
collection or a non-existing collection. If run on a non-existing
collection, the operation creates the collection.
Write Concerns and Transactions
Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
Examples
The examples on this page use data from the sample_mflix sample dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the sample dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.
Update Specific Fields of One Document
Use update operators to update only the specified fields of a document.
For example, documents in the movies collection from the sample_mflix database
contain fields such as title, year, and num_mflix_comments.
The following command uses the $set and $inc
update operators to update the year and the num_mflix_comments fields of a
document where the title equals "The Godfather":
db.runCommand( { update: "movies", updates: [ { q: { title: "The Godfather" }, u: { $set: { year: 1972 }, $inc: { num_mflix_comments: 1 } } } ], ordered: false, writeConcern: { w: "majority", wtimeout: 5000 } } )
Because <update> document does not specify the optional multi
field, the update only modifies one document, even if more than one
document matches the q match condition.
See Output for details.
Update Specific Fields of Multiple Documents
Use update operators to update only the
specified fields of a document, and include the multi field set to
true in the update statement.
For example, documents in the movies collection from the sample_mflix database
contain fields such as year and num_mflix_comments.
The following command uses the $inc update operator to increment
the num_mflix_comments field for all movies released in 1924:
db.runCommand( { update: "movies", updates: [ { q: { year: 1924 }, u: { $inc: { num_mflix_comments: 1 }, $set: { classic: true, era: "silent" } }, multi: true } ], ordered: false, writeConcern: { w: "majority", wtimeout: 5000 } } )
Because the multi field is set to true, the update modifies all 6
documents that match the query specified in the q field and returns the
following output:
{ n: 6, nModified: 6, ok: 1 }
See Output for details.
Update with Aggregation Pipeline
The update command can use an aggregation pipeline for the update.
The pipeline can consist of the following stages:
$addFieldsand its alias$set$replaceRootand its alias$replaceWith
Using the aggregation pipeline allows for a more expressive update statement, such as expressing conditional updates based on current field values or updating one field using the value of another field(s).
Example 1
The following examples uses the aggregation pipeline to modify a field using the values of the other fields in the document.
Documents in the users collection from the sample_mflix database
contain fields such as name and email.
The following update operation uses an aggregation pipeline to add new fields to a specific user's document:
db.runCommand( { update: "users", updates: [ { q: { name: "Robert Baratheon" }, u: [ { $set: { full_info: { $concat: [ "$name", " - ", "$email" ] } } }, { $set: { status: "active" } } ], multi: false } ], ordered: false, writeConcern: { w: "majority", wtimeout: 5000 } } )
Example 2
The aggregation pipeline allows the update to perform conditional updates based on the current field values as well as use current field values to calculate a separate field value.
Documents in the movies collection from the sample_mflix database
have a year field.
The following example uses an aggregation pipeline to calculate the age of "The Great Train Robbery" and assign an era classification based on when it was released.
db.runCommand( { update: "movies", updates: [ { q: { title: "The Great Train Robbery" }, u: [ { $set: { age: { $subtract: [ 2026, "$year" ] } } }, { $set: { era: { $switch: { branches: [ { case: { $lt: [ "$year", 1960 ] }, then: "Classic" }, { case: { $lt: [ "$year", 1980 ] }, then: "Golden Age" }, { case: { $lt: [ "$year", 2000 ] }, then: "Modern" }, { case: { $gte: [ "$year", 2000 ] }, then: "Contemporary" } ], default: "Unknown" } } } } ], multi: false } ], ordered: false, writeConcern: { w: "majority", wtimeout: 5000 } } )
Note
- First Stage
- The
$setstage calculates a new fieldagebased on the difference between 2026 and the movie's release year. See$subtractfor more information. - Second Stage
- The
$setstage calculates a new fielderabased on theyearfield using conditional logic. See$switchfor more information on the$switchaggregation operator.
Bulk Update
The following example performs multiple update operations in a single command to both update existing documents and insert new documents. The operation:
marks highly-rated Horror movies from 2015 as
featuredcategorizes short Drama and Romance movies from 2012 as
melodramaupserts a new Science Fiction movie from 2024 if it doesn't exist
db.runCommand( { update: "movies", updates: [ // Update highly-rated Horror movies from 2015 { q: { year: 2015, genres: "Horror", "imdb.rating": { $gte: 7 } }, u: { $set: { featured: true } }, multi: true }, // Update short Drama/Romance movies from 2012 { q: { year: 2012, genres: { $all: ["Drama", "Romance"] }, runtime: { $lt: 90 } }, u: { $set: { category: "melodrama" } }, multi: true }, // Upsert a new movie from 2026 { q: { title: "A New Movie", year: 2026 }, u: { $set: { genres: ["Sci-Fi", "Adventure"], runtime: 142, "imdb.rating": 8.5, featured: true } }, upsert: true } ], ordered: false, writeConcern: { w: "majority", wtimeout: 5000 } } )
The returned document shows that the command modified existing documents and inserted a new document via upsert. See Output for details.
{ n: 16, upserted: [ { index: 2, _id: ObjectId('69861e680e6ea1f51160fe1c') } ], nModified: 15, ok: 1, '...': '...' }
Specify Collation
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.
Documents in the movies collection from the sample_mflix database
have fields such as title and year.
The following operation uses collation to perform a case-insensitive search.
The query searches for "the godfather" in lowercase, but with
strength: 1 collation, the query matches "The Godfather" regardless of
capitalization:
db.runCommand({ update: "movies", updates: [ { q: { title: "the godfather" }, u: { $set: { featured: true } }, collation: { locale: "en", strength: 1 } } ] })
Specify arrayFilters for Array Update Operations
When updating an array field, you can specify arrayFilters that
determine which array elements to update.
Update Elements Match arrayFilters Criteria
Documents in the movies collection from the sample_mflix database
have a languages array field.
The following example updates all movies that have "English" in their
languages array. The operation replaces "English" with "EN".
db.runCommand( { update: "movies", updates: [ { q: { languages: "English" }, u: { $set: { "languages.$[element]" : "EN" } }, arrayFilters: [ { "element": "English" } ], multi: true} ] } )
Update Specific Elements of an Array of Documents
Documents in the movies collection from the sample_mflix database
have a cast array that lists actor names.
The following example updates all movies that have "Al Pacino" in their
cast array, replacing "Al Pacino" with "REDACTED". The
arrayFilters option specifies which array elements to update:
db.runCommand({ update: "movies", updates: [ { q: { cast: "Al Pacino" }, u: { $set: { "cast.$[elem]" : "REDACTED" } }, arrayFilters: [ { "elem": "Al Pacino" } ], multi: true } ] })
The operation updates all 40 movies that have "Al Pacino" in the cast
array, replacing his name with "REDACTED".
Specify hint for Update Operations
Documents in the movies collection from the sample_mflix database have
fields such as year and num_mflix_comments.
Create the following indexes on the collection:
[ db.movies.createIndex( { year: 1 } ), db.movies.createIndex( { num_mflix_comments: 1 } ) ]
The following update operation increments the num_mflix_comments field for
"The Great Train Robbery" and explicitly hints to use the index { year: 1 }:
Note
If you specify an index that does not exist, the operation errors.
db.runCommand({ update: "movies", updates: [ { q: { title: "The Great Train Robbery" }, u: { $inc: { "num_mflix_comments": 1 } }, hint: { year: 1 }, multi: false } ] })
To see the index used, you can run explain on an update operation.
For example, the following explains an update that increments num_mflix_comments
for movies with 5 or fewer comments released in 2000 or later:
db.runCommand( { explain: { update: "movies", updates: [ { q: { "num_mflix_comments": { $lte: 5 }, "year": { $gte: 2000 } }, u: { $inc: { "num_mflix_comments": 1 } }, hint: { year: 1 }, multi: true } ] }, verbosity: "queryPlanner" } )
The explain does not modify the documents.
Use Variables in let Option or c Field
New in version 5.0.
Variables can be defined in the let option or
the c field and accessed in the updates
array.
Note
To filter results using a variable, you must access the variable
within the $expr operator.
Documents in the movies collection from the sample_mflix database
have fields such as title and year.
The following example uses the let option to define variables for finding
and adding a new field to a movie.
db.runCommand( { update: "movies", updates: [ { q: { $expr: { $eq: [ "$title", "$$movieTitle" ] } }, u: [ { $set: { franchise: "$$franchiseName" } } ] } ], let : { movieTitle: "The Godfather", franchiseName: "The Godfather Trilogy" } } )
The next example defines movieTitle and franchiseName variables in
c and uses the variables to add a franchise field.
db.runCommand( { update: "movies", updates: [ { q: { $expr: { $eq: [ "$title", "$$movieTitle" ] } }, u: [ { $set: { franchise: "$$franchiseName" } } ], c: { movieTitle: "The Godfather", franchiseName: "The Godfather Trilogy" } } ] } )
Output
The returned document contains a subset of the following fields:
update.nAn
updatecommand accepts an array of document updates, some of which can be upserts. For an update,nis the number of documents selected for the update. For an upsert,nis1for the inserted document. The server adds thenvalues for all the updates and upserts and returns the total asupdate.n.If an update operation results in no change to the document, e.g.
$setexpression updates the value to the current value,ncan be greater thannModified.
update.nModifiedThe number of documents updated. If the update operation results in no change to the document, such as setting the value of the field to its current value,
nModifiedcan be less thann.Note
If a subset of matched documents are updated, such as when an update would cause some documents to fail schema validation, the value of
nModifiedreturned by theupdatecommand might not be accurate.
update.upsertedAn array of documents that contains information for each document inserted through the update with
upsert: true.Each document contains the following information:
update.writeErrorsAn array of documents that contains information regarding any error encountered during the update operation. The
writeErrorsarray contains an error document for each update statement that errors.Each error document contains the following fields:
update.writeConcernErrorDocument describing errors that relate to the write concern.
Changed in version 7.0.6: (also available in 6.0.14 and 5.0.30): When
updateexecutes onmongos, write concern errors are always reported, even when one or more write errors occur. In previous releases, the occurrence of write errors could cause theupdateto not report write concern errors.The
writeConcernErrordocuments contain the following fields:update.writeConcernError.errInfo.writeConcernThe write concern object used for the corresponding operation. For information on write concern object fields, see Write Concern Specification.
The write concern object may also contain the following field, indicating the source of the write concern:
update.writeConcernError.errInfo.writeConcern.provenanceA string value indicating where the write concern originated (known as write concern
provenance). The following table shows the possible values for this field and their significance:ProvenanceDescriptionclientSuppliedThe write concern was specified in the application.
customDefaultThe write concern originated from a custom defined default value. See
setDefaultRWConcern.getLastErrorDefaultsThe write concern originated from the replica set's
settings.getLastErrorDefaultsfield.implicitDefaultThe write concern originated from the server in absence of all other write concern specifications.
Changed in version 8.1.2.
When update executes on mongos in a sharded cluster, a writeConcernError is
always reported in the response, even when one or more other errors occur.
In previous releases, other errors sometimes caused update to not report write concern errors.
For example, if a document fails validation, triggering a DocumentValidationFailed error,
and a write concern error also occurs, both the DocumentValidationFailed error and the
writeConcernError are returned in the top-level field of the response.
In addition to the aforementioned update specific return fields, the
db.runCommand() includes additional information:
for replica sets:
optime,electionId,$clusterTime, andoperationTime.for sharded clusters:
operationTimeand$clusterTime.
See db.runCommand Response for details on these fields.
Update Operation with a Sort
Documents in the movies collection from the sample_mflix database
have fields such as year, title, and num_mflix_comments.
The following example finds all movies from 1972 and updates the one with the most comments.
db.runCommand( { update: "movies", updates: [ { // Find movies from 1972 q: { year: 1972 }, // Add a classic_status field to the found movie u: { $set: { classic_status: "Most Discussed 1972 Film" } }, // Only update one movie multi: false, // Sort movies by comment count in descending order sort: { num_mflix_comments: -1 } } ] } )
The operation updates only the 1972 movie with the most comments.