A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-mongodb-update_many-query/ below:

Python MongoDB - Update_many Query

Python MongoDB - Update_many Query

Last Updated : 02 Jul, 2025

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

Explanation:

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

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

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

Salary removed

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



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