Open In App

Python MongoDB - Update_many Query

Last Updated : 02 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In PyMongo, the update_many() method is used to update multiple documents in a collection that match a given filter condition. It’s a powerful method when you need to make bulk updates to documents based on a shared field value or pattern.

Syntax

collection.update_many(

filter,
update,
upsert=False,
array_filters=None,
collation=None,
hint=None

)

Parameters:

Parameter

Type

Description

filter

dict

Query to match documents for update.

update

dict

Update operations (e.g., $set, $inc).

upsert

bool (optional)

Insert if no document matches. Default is False.

array_filters

list (optional)

Conditions to update specific array elements.

collation

Collation (optional)

Language rules for string comparison.

hint

dict or str (optional)

Index to optimize query performance.

Here is our sample data:

Python
from pymongo import MongoClient

c = MongoClient("mongodb://localhost:27017/")
db = c['companyDB']
col = db['employees']

data = [
    {"_id": 1, "name": "Alice", "department": "HR", "salary": 30000},
    {"_id": 2, "name": "Bob", "department": "Engineering", "salary": 50000},
    {"_id": 3, "name": "Charlie", "department": "Engineering", "salary": 48000},
    {"_id": 4, "name": "David", "department": "HR", "salary": 32000},
    {"_id": 5, "name": "Eve", "department": "Marketing", "salary": 40000}
]

col.delete_many({})
col.insert_many(data)
print("Data inserted.")

Output

Data inserted.

Sample_data
Sample data

Explanation:

  • MongoClient() connects to the local server and accesses companyDB.employees.
  • insert_many(data) inserts sample employee records.
  • delete_many({}) ensures no duplicate data from previous runs.

Examples

Example 1: Increase salary by 10% for all Engineering department employees

Python
from pymongo import MongoClient

c = MongoClient("mongodb://localhost:27017/")
db = c['companyDB']
col = db['employees']

res = col.update_many(
    {"department": "Engineering"},
    { "$mul": { "salary": 1.1 } }  # multiply salary by 1.1 (10% raise)
)

print(f"Matched: {res.matched_count}, Modified: {res.modified_count}")

Output

Output
Output
Salary raised

Explanation: Matches all documents where department is "Engineering". $mul increases salary by 10% and update_many modifies all matching documents.

Example 2: Set a status field to "Active" for all employees in HR

Python
from pymongo import MongoClient

c = MongoClient("mongodb://localhost:27017/")
db = c['companyDB']
col = db['employees']
res = col.update_many(
    {"department": "HR"},
    { "$set": { "status": "Active" } }
)

print(f"Matched: {res.matched_count}, Modified: {res.modified_count}")

Output

Matched: 2, Modified: 2

Output
Status updated

Explanation: Adds a new field status and sets its value to "Active" for HR employees and $set is used to update/add a field.

Example 3: Remove the "salary" field from all Marketing department employees

Python
from pymongo import MongoClient

c = MongoClient("mongodb://localhost:27017/")
db = c['companyDB']
col = db['employees']

res = col.update_many(
    {"department": "Marketing"},
    { "$unset": { "salary": "" } }
)

print(f"Matched: {res.matched_count}, Modified: {res.modified_count}")

Output

Matched: 1, Modified: 1

Output
Salary removed

Explanation: $unset removes the specified field from the document.


Next Article
Article Tags :
Practice Tags :

Similar Reads