How to update data in a Collection using Python?

Last Updated : 12 Jul, 2025

MongoDB is a cross-platform, document-oriented database that works on the concept of collections and documents. MongoDB offers high speed, high availability, and high scalability.

Updating Data in MongoDB

We can update data in a collection using update_one() method and update_many() method.Ā 
Ā 

update_one()Ā 

update_one() method update first occurrence if document matching the query filter is found.Ā 

Syntax :update_one(query, newvalues, upsert=False, bypass_document_validation=False, collation=None, array_filters=None, session=None)
Ā 

Parameters:Ā 

filter : A query that matches the document to update.
new_values : The modifications to apply.
upsert (optional): If ā€œTrueā€, perform an insert if no documents match the filter.
bypass_document_validation (optional) : If ā€œTrueā€, allows the write to opt-out of document level validation. Default is ā€œFalseā€.
collation (optional) : An instance of class: ā€˜~pymongo.collation.Collation’. This option is only supported on MongoDB 3.4 and above.
array_filters (optional) : A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+.
session (optional) : a class:’~pymongo.client_session.ClientSession’.

hint (optional): An index to use to support the query predicate specified. This option is only supported on MongoDB 4.2 and above.

Example:

Sample database is as follows:Ā 

Python3
import pymongo


client = pymongo.MongoClient("mongodb://localhost:27017/")

# Database name
db = client["GFG"]

# Collection name
col = db["gfg"]

# Query to be updated
query = {"coursename": "SYSTEM DESIGN"}

# New value
newvalue = {"$set": {"coursename": "Computer network"}}

# Update the value
col.update_one(query, newvalue)

Output:

Method: update_many()Ā 

update_many() method update all the documents matching the query filter.Ā 
Ā 

Syntax:Ā 

update_many(query, newvalues, upsert=False, bypass_document_validation=False, 
            collation=None, array_filters=None, session=None)


Parameters:

  • ā€˜filter’ : A query that matches the document to update.
  • ā€˜new_values’ : The modifications to apply.
  • ā€˜upsert’ (optional): If ā€œTrueā€, perform an insert if no documents match the filter.
  • ā€˜bypass_document_validation’ (optional) : If ā€œTrueā€, allows the write to opt-out of document level validation. Default is ā€œFalseā€.
  • ā€˜collation’ (optional) : An instance of class: ā€˜~pymongo.collation.Collation’. This option is only supported on MongoDB 3.4 and above.
  • ā€˜array_filters’ (optional) : A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+.
  • ā€˜session’ (optional) : a class:’~pymongo.client_session.ClientSession’.

Example:

Python3
import pymongo


client = pymongo.MongoClient("mongodb://localhost:27017/")

# Database name
db = client["GFG"]

# Collection name
col = db["gfg"]

# Query to be updated
query = {"coursename": "SYSTEM DESIGN"}

# New value
newvalue = {"$set": {"coursename": "Computer network"}}

# Update the value
col.update_many(query, newvalue)

Output:

Comment