- MongoDB CRUD Operations >
- Update Documents
Update Documents¶
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
This page provides examples of how to update documents in
using the following methods in the mongo
shell:
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <replacement>, <options>)
This page provides examples of how to update documents using the following methods in the PyMongo Python driver:
pymongo.collection.Collection.update_one()
pymongo.collection.Collection.update_many()
pymongo.collection.Collection.replace_one()
This page provides examples of how to update documents using the following methods in the Java Synchronous Driver:
This page provides examples of how to update documents using the following methods in the MongoDB Node.js Driver:
This page provides examples of how to update documents using the following methods in the MongoDB PHP Library:
MongoDB\Collection::updateOne()
MongoDB\Collection::updateMany()
MongoDB\Collection::replaceOne()
This page provides examples of how to update documents using the following methods in the Java Reactive Streams Driver:
This page provides examples of how to update documents using the following methods in the MongoDB C# Driver:
This page provides examples of how to update documents using the following methods in the MongoDB Perl Driver:
This page provides examples of how to update documents using the following methods in the MongoDB Ruby Driver:
This page provides examples of how to update documents using the following methods in the MongoDB Scala Driver:
The examples on this page use the inventory
collection. To create
and/or populate the inventory
collection, run the following:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
You can run the operation in the web shell below:
Update Documents in a Collection¶
To update a document, MongoDB provides update operators, such as $set
, to modify field
values.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
To use the update operators, pass to the update methods an update document of the form:
To use the update operators, pass to the update methods an update document of the form:
The driver provides the com.mongodb.client.model.Updates class to facilitate the creation of update documents. For example:
For a list of the update helpers, see com.mongodb.client.model.Updates.
To use the update operators, pass to the update methods an update document of the form:
The driver provides the com.mongodb.client.model.Updates class to facilitate the creation of update documents. For example:
For a list of the update helpers, see com.mongodb.client.model.Updates.
To use the update operators, pass to the update methods an update document of the form:
To use the update operators, pass to the update methods an update document of the form:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual update operator reference for details.
Update a Single Document¶
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
The following example uses the
db.collection.updateOne()
method on the
inventory
collection to update the first document where
item
equals "paper"
:
The following example uses the
update_one()
method on
the inventory
collection to update the first document
where item
equals "paper"
:
The following example uses the
com.mongodb.client.MongoCollection.updateOne method on the
inventory
collection to update the first document where
item
equals "paper"
:
The following example uses the
Collection.updateOne()
method on the inventory
collection to update the first
document where item
equals "paper"
:
The following example uses the
updateOne()
method on the inventory
collection to update the first
document where item
equals "paper"
:
The following example uses the
com.mongodb.reactivestreams.client.MongoCollection.updateOne
on the inventory
collection to update the first document where
item
equals "paper"
:
The following example uses the
IMongoCollection.UpdateOne() method on the
inventory
collection to update the first document where
item
equals "paper"
:
The following example uses the
update_one() method on the
inventory
collection to update the first document where
item
equals "paper"
:
The following example uses the
update_one() method on the
inventory
collection to update the first document where
item
equals "paper"
:
The following example uses the
updateOne()
method on the inventory
collection to update the first
document where item
equals "paper"
:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
The update operation:
- uses the
$set
operator to update the value of thesize.uom
field to"cm"
and the value of thestatus
field to"P"
, - uses the
$currentDate
operator to update the value of thelastModified
field to the current date. IflastModified
field does not exist,$currentDate
will create the field. See$currentDate
for details.
Update Multiple Documents¶
New in version 3.2.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
The following example uses the db.collection.updateMany()
method on the inventory
collection to update all documents where
qty
is less than 50
:
The following example uses the
update_many()
method on
the inventory
collection to update all documents where
qty
is less than 50
:
The following example uses the
com.mongodb.client.MongoCollection.updateMany method on
the inventory
collection to update all documents where
qty
is less than 50
:
The following example uses the
Collection.updateMany()
method on the inventory
collection to update all documents
where qty
is less than 50
:
The following example uses the
updateMany()
method on the inventory
collection to update all documents
where qty
is less than 50
:
The following example uses the
com.mongodb.reactivestreams.client.MongoCollection.updateMany
method on the inventory
collection to update all documents where
qty
is less than 50
:
The following example uses the
IMongoCollection.UpdateMany()
method on the inventory
collection to update all documents
where qty
is less than 50
:
The following example uses the
update_many() method on
the inventory
collection to update all documents where
qty
is less than 50
:
The following example uses the
update_many() method on
the inventory
collection to update all documents where
qty
is less than 50
:
The following example uses the
updateMany() method on
the inventory
collection to update all documents where
qty
is less than 50
:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
The update operation:
- uses the
$set
operator to update the value of thesize.uom
field to"in"
and the value of thestatus
field to"P"
, - uses the
$currentDate
operator to update the value of thelastModified
field to the current date. IflastModified
field does not exist,$currentDate
will create the field. See$currentDate
for details.
Replace a Document¶
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
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()
.
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
replace_one()
.
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
com.mongodb.client.MongoCollection.replaceOne.
To replace the entire content of a document except for the
_id
field, pass an entirely new document as the second
argument to
Collection.replaceOne().
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
replaceOne()
.
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
com.mongodb.reactivestreams.client.MongoCollection.replaceOne.
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
IMongoCollection.ReplaceOne().
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
replace_one().
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
replace_one().
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to
replaceOne()
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not 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
inventory
collection that matches the filter item
equals
"paper"
:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
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.
Document Size¶
For MMAPv1, when performing update operations that increase the document size beyond the allocated space for that document, the update operation relocates the document on disk.
Field Order¶
MongoDB preserves the order of the document fields following write operations 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.
Changed in version 2.6: Starting in version 2.6, MongoDB actively attempts to preserve the field order in a document. Before version 2.6, MongoDB did not actively preserve the order of the fields in a document.
Upsert
Option¶
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
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.
If update_one()
,
update_many()
, or
replace_one()
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.
If the update and replace methods include the com.mongodb.client.model.UpdateOptions parameter that specifies com.mongodb.client.model.UpdateOptions.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.
If updateOne(),
updateMany(), or
replaceOne() include
upsert : true
in the options
parameter document 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.
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.
If the update and replace methods include the UpdateOptions parameter that specifies UpdateOptions.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.
If UpdateOne(),
UpdateMany(), or
ReplaceOne()
includes an UpdateOptions
argument instance with the IsUpsert
option set to 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.
If update_one(),
update_many(), or
replace_one() 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.
If update_one(),
update_many(), or
replace_one() 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.
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.
Write Acknowledgement¶
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
See also
pymongo.collection.Collection.update_one()
pymongo.collection.Collection.update_many()
pymongo.collection.Collection.replace_one()
- Additional Methods
See also
MongoDB\Collection::updateOne()
MongoDB\Collection::updateMany()
MongoDB\Collection::replaceOne()
- Additional Methods