Menu Close

Python MongoDB Delete

Python MongoDB Delete

In this Python MongoDB tutorial, you will learn everything about Python MongoDB Delete to delete document one or multiple documents from the MongoDB collection. In the previous tutorial, we have seen all about how to filter documents in MongoDB using Python. Python PyMongo provides some functions to delete documents from MongoDB.

Python MongoDB Delete

Python PyMongo provide two functions delete_one() and delete_many() to delete documents from MongoDB database.

  1. delete_one():- The delete_one() method is used to only one document from the MongoDB.The first parameter of delete_one() method is query object that define which document you want to delete.
  2. delete_many():- The delete_many() method is used to delete more than one documents from MongoDB database.The first parameter of delete_one() method is query object that define which document you want to delete.

Note:- If the query finds more than one document, only the first occurrence is deleted.

Delete Single MongoDB Document

To delete a single MongoDB document use delete_one() method.

Example: How to delete a Document in MongoDB using Python


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

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

query = {"age" : 24}
x = collection.delete_one(query)
print(x)

Output

<pymongo.results.DeleteResult object at 0x00000270C8430EC0>

When you execute the above code, The only first document will be deleted whose age is 24.

Delete Multiple Documents

You can also delete more than one documents from the MongoDB collection. To delete all the documents from the MongoDB collections you need to pass the query object in the delete_many() method that defines which document you want to delete.

Example:- Delete Multiple Document from MongoDB

In this example, you delete all the employees were age is 30.


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

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

query = {"age" : 30}
collection.delete_many(query)

When the above code is executed successfully all the documents will be deleted whose age will be 30.

Delete All Documents

You can also delete all the documents from the MongoDB collection. To delete all the documents from the MongoDB collections you need to pass an empty dictionary in the delete_many() method.

Example: Delete all Documents


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

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

query = {"age" : 30}
collection.delete_many({})

When the above code is executed all the documents from the MongoDB employees collections will be deleted.

Conclusion

So, In this article, we have done Python MongoDB delete to delete single and multiple documents. Python PyMongo provides the best functions to delete one and more documents.
You can use any delete_one() method to delete only one documents from MongoDB.But if you want to delete more than documents from MongoDB collections you can go with the delete_many() function.

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

Reference:- Click Here

Thanks for reading……

How to Filter Records in MongoDB using Python ?
Python MongoDB Drop

Related Posts