Menu Close

Python MongoDB Update

Python MongoDB Update

In this article, you will learn everything about Python MongoDB Update to update single documents and multiple documents with the help of examples. In the previous tutorial, we have seen all about how to drop the MongoDB collection.

Python PyMongo driver two methods to update MongoDB documents. Here, In this Python MongoDB Update Document guide, we will see two methods to update single documents and multiple documents with the help of the example.

Python MongoDB Update

PyMongo provides two methods to update single or multiple MongoDB documents on the basis of condition. Two methods are update_one() and update_many().

Update a MongoDB Document using Python

Python PyMongo MongoDB database driver provides a method called update_one() to update only a single document. The update_one() method receives two parameters first is a query that specifies which document you want to update and the second parameter specifies a new value. Both the parameters should be Python objects.

Note:- If the query finds multiple documents, the only first occurrence is updated.

Example: Python MongoDB Update a Document


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

query = {"_id": 1}
new_values = {"$set": {"name": "Vishvajit"}}

#update the document
collection.update_one(query, new_values)

# access all the documents
for i in collection.find():
    print(i)

Output


{'_id': 1, 'name': 'Vishvajit', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': 2, 'name': 'Peter', 'Occupation': 'DBA', 'age': 33}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}
{'_id': 4, 'name': 'Harry', 'Occupation': 'Python Dev', 'age': 35}

Update Many MongoDB Documents using Python

To update many MongoDB documents, PyMongo provides update_many() a method. The update_many() method takes the same parameters which take the update_one() method. The update_many() method all the documents that match the condition specified in the first parameter or query object.

Example: Python MongoDB Update Many Document


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

query = {"age": 30}
new_values = {"$set": {"name": "Vishvajit"}}

#update the document
collection.update_many(query, new_values)

# access all the documents
for i in collection.find():
    print(i)

Output


{'_id': 1, 'name': 'Vishvajit', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': 2, 'name': 'Peter', 'Occupation': 'DBA', 'age': 33}
{'_id': 3, 'name': 'Vishvajit', 'Occupation': 'Machine Learning Enginner', 'age': 30}
{'_id': 4, 'name': 'Harry', 'Occupation': 'Python Dev', 'age': 35}

When the above code gets executed successfully, all the documents are successfully updated whose age is 30.

Conclusion

So, In this Python MongoDB Update tutorial, we have seen a total of two ways to update MongoDB single and multiple documents. I always prefer, when you want to update only one MongoDB document, to use the update_one() method and if you want to update more than one document in MongoDB using Python always go with update_many() method.

I hope this article will be helpful for you. If you like this article, please share, support, and keep visiting for further Python MongoDB Tutorials.

Thanks for your valuable time …

Reference:- Click Here

Create MongoDB Database in Python
Python MongoDB Limit

Related Posts