Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Get all the Documents of the Collection using PyMongo
Next article icon

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
importpymongoclient=pymongo.MongoClient("mongodb://localhost:27017/")# Database namedb=client["GFG"]# Collection namecol=db["gfg"]# Query to be updatedquery={"coursename":"SYSTEM DESIGN"}# New valuenewvalue={"$set":{"coursename":"Computer network"}}# Update the valuecol.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
importpymongoclient=pymongo.MongoClient("mongodb://localhost:27017/")# Database namedb=client["GFG"]# Collection namecol=db["gfg"]# Query to be updatedquery={"coursename":"SYSTEM DESIGN"}# New valuenewvalue={"$set":{"coursename":"Computer network"}}# Update the valuecol.update_many(query,newvalue)

Output:


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