Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
MongoDB Python - Insert and Replace Operations
Next article icon

In PyMongo, theupdate_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
frompymongoimportMongoClientc=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 by10% for all Engineering department employees

Python
frompymongoimportMongoClientc=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% andupdate_many modifies all matching documents.

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

Python
frompymongoimportMongoClientc=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 toupdate/add a field.

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

Python
frompymongoimportMongoClientc=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: $unsetremoves the specified field from the document.


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp