In this guide, you can learn how to use PyMongo to update documents in a MongoDB collection by using the update_one()
or update_many()
methods.
The examples in this guide use the sample_restaurants.restaurants
collection from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with PyMongo tutorial.
You can perform update operations in MongoDB by using the following methods:
update_one()
, which updates the first document that matches the search criteria
update_many()
, which updates all documents that match the search criteria
Each update method requires the following parameters:
A query filter document, which determines which documents to update. For more information about query filters, see the Query Filter Documents section in the MongoDB Server manual.
An update document, which specifies the update operator (the kind of update to perform) and the fields and values that should change. For a list of update operators and their usage, see the Field Update Operators guide page in the MongoDB Server manual.
The following example uses the update_one()
method to update the name
value of a document named "Bagels N Buns"
in the restaurants
collection. Select the Synchronous or Asynchronous tab to see the corresponding code:
restaurants = database["restaurants"]query_filter = {'name' : 'Bagels N Buns'}update_operation = { '$set' : { 'name' : '2 Bagels 2 Buns' }}result = restaurants.update_one(query_filter, update_operation)
restaurants = database["restaurants"]query_filter = {'name' : 'Bagels N Buns'}update_operation = { '$set' : { 'name' : '2 Bagels 2 Buns' }}result = await restaurants.update_one(query_filter, update_operation)
The following example uses the update_many()
method to update all documents with a cuisine
value of "Pizza"
. After the update, the documents have a cuisine
value of "Pasta"
. Select the Synchronous or Asynchronous tab to see the corresponding code:
restaurants = database["restaurants"]query_filter = {'cuisine' : 'Pizza'}update_operation = { '$set' : { 'cuisine' : 'Pasta' }}result = restaurants.update_many(query_filter, update_operation)
restaurants = database["restaurants"]query_filter = {'cuisine' : 'Pizza'}update_operation = { '$set' : { 'cuisine' : 'Pasta' }}result = await restaurants.update_many(query_filter, update_operation)
The update_one()
and update_many()
methods optionally accept additional parameters, which represent options you can use to configure the update operation. If you don't specify any additional options, the driver does not customize the update operation.
Property
Description
upsert
Specifies whether the update operation performs an upsert operation if no documents match the query filter. For more information, see the
upsert statementin the MongoDB Server manual.
Defaults to False
bypass_document_validation
Specifies whether the update operation bypasses document validation. This lets you update documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see
Schema Validationin the MongoDB Server manual.
Defaults to False
.
collation
Specifies the kind of language collation to use when sorting results. See
Collationfor more information.
array_filters
A list of filters that specifies which array elements an update applies to.
hint
Gets or sets the index to scan for documents. For more information, see the
hint statementin the MongoDB Server manual.
session
An instance of ClientSession
.
let
A Map of parameter names and values. Values must be constant or closed expressions that don't reference document fields. For more information, see the
let statementin the MongoDB Server manual.
comment
A comment to attach to the operation. For more information, see the
insert command fieldsguide in the MongoDB Server manual for more information.
The following code uses the update_many()
method to find all documents where the borough
field has the value "Manhattan"
. It then updates the borough
value in these documents to "Manhattan (north)"
. Because the upsert
option is set to True
, PyMongo inserts a new document if the query filter doesn't match any existing documents. Select the Synchronous or Asynchronous tab to see the corresponding code:
restaurants = database["restaurants"]query_filter = {'borough' : 'Manhattan'}update_operation = { '$set' : { 'borough' : 'Manhattan (north)' }}result = restaurants.update_many(query_filter, update_operation, upsert = True)
restaurants = database["restaurants"]query_filter = {'borough' : 'Manhattan'}update_operation = { '$set' : { 'borough' : 'Manhattan (north)' }}result = await restaurants.update_many(query_filter, update_operation, upsert = True)
When you perform an update operation, you can specify a collation for the driver to use.
A collation is a set of language-specific rules for string comparison, such as for letter case and accent marks.
To specify a collation, create an instance of the Collation
class or a Python dictionary. For a list of options to pass to the Collation
constructor or include as keys in the dictionary, see Collation in the MongoDB Server manual.
To create an instance of the Collation
class, you must import it from pymongo.collation
.
The following example performs the same update operation as the previous example, but with a default collation of fr_CA
. Select the Synchronous or Asynchronous tab to see the corresponding code:
from pymongo.collation import Collationrestaurants = database["restaurants"]query_filter = {'cuisine' : 'Pizza'}update_operation = { '$set' : { 'cuisine' : 'Pasta' }}result = restaurants.update_many(query_filter, update_operation, collation=Collation(locale='fr_CA'))
from pymongo.collation import Collationrestaurants = database["restaurants"]query_filter = {'cuisine' : 'Pizza'}update_operation = { '$set' : { 'cuisine' : 'Pasta' }}result = await restaurants.update_many(query_filter, update_operation, collation=Collation(locale='fr_CA'))
Note Operation Collation Overrides Default
When you specify a collation as part of an operation, it overrides the default collation for the collection.
The update_one()
and update_many()
methods each return an UpdateResult
object. The UpdateResult
type contains the following properties:
Property
Description
matched_count
The number of documents that matched the query filter, regardless of how many were updated.
modified_count
The number of documents modified by the update operation. If an updated document is identical to the original, it is not included in this count.
raw_result
The raw result document returned by the server.
upserted_id
The ID of the document that was upserted in the database, if the driver performed an upsert. Otherwise None
.
To learn more about creating query filters, see the Specify a Query guide.
To learn more about any of the methods or types discussed in this guide, see the following API documentation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4