Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to update data in a Collection using Python?

How to update data in a Collection using Python?

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




import pymongo
 
 
client = pymongo.MongoClient("mongodb://localhost:27017/")
 
# Database name
db = client["GFG"]
 
# Collection name
col = db["gfg"]
 
# Query to be updated
query = {"coursename": "SYSTEM DESIGN"}
 
# New value
newvalue = {"$set": {"coursename": "Computer network"}}
 
# Update the value
col.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




import pymongo
 
 
client = pymongo.MongoClient("mongodb://localhost:27017/")
 
# Database name
db = client["GFG"]
 
# Collection name
col = db["gfg"]
 
# Query to be updated
query = {"coursename": "SYSTEM DESIGN"}
 
# New value
newvalue = {"$set": {"coursename": "Computer network"}}
 
# Update the value
col.update_many(query, newvalue)


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments