- MongoDB CRUD Operations >
- Update Documents
Update Documents¶
This page provides examples in:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
This page uses the following mongo
shell methods:
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <update>, <options>)
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses MongoDB Compass to update the documents.
The examples on this page use the inventory
collection.
Populate the inventory
collection with the following
documents:
This page uses the following PyMongo Python driver methods:
pymongo.collection.Collection.update_one()
pymongo.collection.Collection.update_many()
pymongo.collection.Collection.replace_one()
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following Java Synchronous Driver methods:
- com.mongodb.client.MongoCollection.updateOne
- com.mongodb.client.MongoCollection.updateMany
- com.mongodb.client.MongoCollection.replaceOne
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following MongoDB Node.js Driver methods:
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following MongoDB PHP Library methods:
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following Motor driver methods:
motor.motor_asyncio.AsyncIOMotorCollection.update_one()
motor.motor_asyncio.AsyncIOMotorCollection.update_many()
motor.motor_asyncio.AsyncIOMotorCollection.replace_one()
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following Java Reactive Streams Driver methods:
- com.mongodb.reactivestreams.client.MongoCollection.updateOne
- com.mongodb.reactivestreams.client.MongoCollection.updateMany
- com.mongodb.reactivestreams.client.MongoCollection.replaceOne
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following MongoDB C# Driver methods:
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following MongoDB Perl Driver methods:
- MongoDB::Collection::update_one()
- MongoDB::Collection::update_many()
- MongoDB::Collection::replace_one()
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following MongoDB Ruby Driver methods:
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following MongoDB Scala Driver methods:
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
This page uses the following MongoDB Go Driver functions:
The examples on this page use the
inventory
collection. To create and/or populate the
inventory
collection, run the following:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
You can run the operation in the web shell below:
For instructions on inserting documents using MongoDB Compass, see Insert Documents.
Update Documents in a Collection¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document in Compass, hover over the target document and click the pencil icon:
After clicking the pencil icon, the document enters edit mode:
You can now change the this document by clicking the item you wish to change and modifying the value.
For detailed instructions on updating documents in Compass, refer to the Compass documentation or follow the example below.
Once you are satisfied with your changes, click Update to save the updated document.
Click Cancel to revert any modifications made to the document and exit edit mode.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
update operators such
as $set
to modify field values.
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.
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
update operators such
as $set
to modify field values.
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.
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
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:
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
To update a document, MongoDB provides
update operators such
as $set
to modify field values.
Some update operators, such as $set
, will create the field if
the field does not exist. See the individual
update operator reference for
details.
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.
Update a Single Document¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- 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 demonstrates using MongoDB Compass to modify
a single document where item: paper
in the inventory
collection:
Note
This example uses the Compass Table View to modify the document. The editing process using the Compass List View follows a very similar approach.
For more information on the differences between Table View and List View in Compass, refer to the Compass documentation.
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
update_one()
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"
:
The following example uses the
Collection.UpdateOne method on the
inventory
collection to update the first document where
item
equals "paper"
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- 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.
Modify the target document as follows:
- Change the
status
field fromD
toP
. - Change the
size.uom
field fromin
tocm
. - Add a new field called
lastModified
whose value will be today’s date.
Click the Table button in the top navigation to access the Table View:
Use the Compass query bar to locate the target document.
Copy the following filter document into the query bar and click Find:
Hover over the
status
field and click the pencil icon which appears on the right side of the document to enter edit mode:Change the value of the field to
"P"
.Click the Update button below the field to save your changes.
Hover over the
size
field and click the outward-pointing arrows which appear on the right side of the field. This opens a new tab which displays the fields within thesize
object:Using the same process outlined in steps 3-5 for editing the
status
field, change the value of thesize.uom
field to"cm"
.Click the left-most tab above the table labelled
inventory
to return to the original table view, which displays the top-level document:Hover over the
status
field and click the pencil icon which appears on the right side of the document to re-enter edit mode.Click inside of the
status
field and click the plus button icon which appears in the edit menu.Click the Add Field After status button which appears below the plus button:
Add a new field called
lastModified
with a value of today’s date. Set the field type toDate
:Click the Update button below the field to save your changes.
Note
Because MongoDB Compass does not support
$currentDate
or any other Field Update Operators, you must manually enter the date value in Compass.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
Update Multiple Documents
New in version 3.2.
The following example uses the db.collection.updateMany()
method on the inventory
collection to update all documents where
qty
is less than 50
:
Update Multiple Documents
New in version 3.2.
The following example uses the
update_many()
method on
the inventory
collection to update all documents where
qty
is less than 50
:
Update Multiple Documents
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
:
Update Multiple Documents
The following example uses the
Collection.updateMany()
method on the inventory
collection to update all documents
where qty
is less than 50
:
Update Multiple Documents
The following example uses the
updateMany()
method on the inventory
collection to update all documents
where qty
is less than 50
:
Update Multiple Documents
New in version 3.2.
The following example uses the
update_many()
method on the inventory
collection to update all documents
where qty
is less than 50
:
Update Multiple Documents
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
:
Update Multiple Documents
The following example uses the
IMongoCollection.UpdateMany()
method on the inventory
collection to update all documents
where qty
is less than 50
:
Update Multiple Documents
The following example uses the
update_many() method on
the inventory
collection to update all documents where
qty
is less than 50
:
Update Multiple Documents
The following example uses the
update_many() method on
the inventory
collection to update all documents where
qty
is less than 50
:
Update Multiple Documents
The following example uses the
updateMany() method on
the inventory
collection to update all documents where
qty
is less than 50
:
Update Multiple Documents
The following example uses the
Collection.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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
Replace a Document
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; 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 where item: "paper"
:
Replace a Document
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()
.
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 where item: "paper"
:
Replace a Document
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.
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 where item: "paper"
:
Replace a Document
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().
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 where item: "paper"
:
Replace a Document
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 where item: "paper"
:
Replace a Document
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()
.
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 where item: "paper"
:
Replace a Document
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.
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 where item: "paper"
:
Replace a Document
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().
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 where item: "paper"
:
Replace a Document
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().
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 where item: "paper"
:
Replace a Document
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().
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 where item: "paper"
:
Replace a Document
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 where item: "paper"
:
Replace a Document
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.
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 where item: "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.
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.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
Upsert Option
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.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
For details on the new document created, see the individual reference pages for the methods.
Upsert Option
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.
Upsert Option
If Collection.UpdateOne includes the Upsert 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.
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
- Compass
- Python
- Java (Sync)
- Node.js
- Other
See also
See also
motor.motor_asyncio.AsyncIOMotorCollection.update_one()
motor.motor_asyncio.AsyncIOMotorCollection.update_many()
motor.motor_asyncio.AsyncIOMotorCollection.replace_one()
- Additional Methods