Python MongoDB - Update_many Query
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:
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.

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
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


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
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

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
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

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