Prerequisite : MongoDB Basics, Insert and Update
Aim : To delete entries/documents of a collection in a database. Assume name of collection ‘my_collection’.
Method used : delete_one() or delete_many()
- Remove All Documents That Match a Condition : The following operation removes all documents that match the specified condition.
result = my_collection.delete_many({"name": "Mr.Geek"}) - To see the number of documents deleted :
print(result.deleted_count)
- Remove All Documents :
Method 1 : Remove all documents using delete_many()result= my_collection.delete_many({})Method 2 : Delete all documents using collection.remove()
result = my_collection.remove()
Best method to remove is to drop the collection so that data indexes are also removed and then create a new collection in that insert data.
- To Drop a Collection :
db.my_collection.drop()
We first insert a document in the collection then deleted the documents as per query.
# Python program to illustrate # delete, drop and remove from pymongo import MongoClient try: conn = MongoClient() print("Connected successfully!!!") except: print("Could not connect to MongoDB") # database db = conn.database # Created or Switched to collection names: my_gfg_collection collection = db.my_gfg_collection emp_rec1 = { "name":"Mr.Geek", "eid":24, "location":"delhi" } emp_rec2 = { "name":"Mr.Shaurya", "eid":14, "location":"delhi" } emp_rec3 = { "name":"Mr.Coder", "eid":14, "location":"gurugram" } # Insert Data rec_id1 = collection.insert_one(emp_rec1) rec_id2 = collection.insert_one(emp_rec2) rec_id3 = collection.insert_one(emp_rec3) print("Data inserted with record ids",rec_id1," ",rec_id2,rec_id3) # Printing the document before deletion cursor = collection.find() for record in cursor: print(record) # Delete Document with name : Mr Coder result = collection.delete_one({"name":"Mr.Coder"}) # If query would have been delete all entries with eid:14 # use this # result = collection.delete_many("eid":14}) cursor = collection.find() for record in cursor: print(record) |
OUTPUT (comment line denoted by #)
Connected successfully!!!
Data inserted with record ids
#Data INSERT
{'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name':
'Mr.GfG', 'eid': 45, 'location': 'noida'}
{'_id': ObjectId('5a0c734937b8551c1cd03349'), 'name':
'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}
{'_id': ObjectId('5a0c734937b8551c1cd0334a'), 'name':
'Mr.Coder', 'eid': 14, 'location': 'gurugram'}
#Mr.Coder is deleted
{'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name':
'Mr.GfG', 'eid': 45, 'location': 'noida'}
{'_id': ObjectId('5a0c734937b8551c1cd03349'), 'name':
'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}
Recommended Posts:
- Drop Collection if already exists in MongoDB using Python
- How to Drop all the indexes in a Collection using PyMongo?
- How to access a collection in MongoDB using Python?
- How to create index for MongoDB Collection using Python?
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- MongoDB Python | Insert and Update Data
- How to update data in a Collection using Python?
- 3D Plotting sample Data from MongoDB Atlas Using Python
- How to fetch data from MongoDB using Python?
- How to delete data from file in Python
- Python | Pandas Index.drop()
- Python | Pandas Series.drop()
- Python | Drop-down list in kivy using .kv file
- Python MySQL - Drop Table
- Python - Channel Drop using Pillow
- PyQt5 QCalendarWidget - How to drag and drop it anywhere in the window using mouse
- MongoDB and Python
- Garbage Collection in Python
- PyQt5 ComboBox â User entered items not stored in drop down menu
- How to drop one or multiple columns in Pandas Dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

