Delete and Drop method in MongoDB using Python
Hello programmers, here we are going to take a look at how to delete documents from the collection along with that we will also take a look at how to drop a collection in MongoDB using Python.
Until now we have covered some of the methods in MongoDB which are listed below. I would recommend you to check them out first.
- How to create a database: How to create a database in MongoDB using Python
- How to insert data into a collection: How to create collections and insert data to collection in MongoDB in Python
- How to select data from the collection: How to select data from collections in MongoDB
- How to update data from the collection: How to update documents form collection in MongoDB
Delete method in MongoDB using Python
Similar to all other methods, to delete a document we have two methods. To delete single document from collection we use delete_one() method. To delete more than one document we use the delete_many() method. We can pass some queries as a parameter to the delete_many() to delete only selected documents from the collection.
Firstly, the database I am working with as given below.
{'_id': ObjectId('5dee3c828d178a953eec2ad7'), 'age': 21, 'author': 'Shailesh Bhimanpelli', 'course': 'MongoDB using Python', 'rating': 1} {'_id': ObjectId('5dee451be59a39bf5e67a6f9'), 'age': 25, 'author': 'Saruque Ahamed Mollick', 'course': 'Python, Java, C++', 'rating': 5} {'_id': ObjectId('5dee451be59a39bf5e67a6fa'), 'age': 23, 'author': 'Ben Thomas', 'course': 'HTML, CSS', 'rating': 3} {'_id': ObjectId('5dee4556df618453e14b243c'), 'age': 25, 'author': 'Varun Thakur', 'course': 'JavaScript, Python', 'rating': 5} {'_id': ObjectId('5def3b1b2a61b9be930647ff'), 'age': 30, 'author': 'Sorabh Pant', 'course': 'Java', 'rating': 3} {'_id': ObjectId('5def3b1b2a61b9be93064800'), 'age': 27, 'author': 'Sahil Shah', 'course': 'jQuery', 'rating': 3} {'_id': ObjectId('5def3b1b2a61b9be93064801'), 'age': 21, 'author': 'Joel Smith', 'course': 'Wordpress', 'rating': 2}
delete_one() method MongoDB
It will delete a single document from the collection which meets the condition specified in the query. If there are multiple documents that satisfy the condition specified in the query then the first occurring document will be deleted.
result = {'author': 'Joel Smith'} courses.delete_one(result) for i in courses.find(): print(i)
Output
{'_id': ObjectId('5dee3c828d178a953eec2ad7'), 'age': 21, 'author': 'Shailesh Bhimanpelli', 'course': 'MongoDB using Python', 'rating': 1} {'_id': ObjectId('5dee451be59a39bf5e67a6f9'), 'age': 25, 'author': 'Saruque Ahamed Mollick', 'course': 'Python, Java, C++', 'rating': 5} {'_id': ObjectId('5dee451be59a39bf5e67a6fa'), 'age': 23, 'author': 'Ben Thomas', 'course': 'HTML, CSS', 'rating': 4} {'_id': ObjectId('5dee4556df618453e14b243c'), 'age': 25, 'author': 'Varun Thakur', 'course': 'JavaScript, Python', 'rating': 5} {'_id': ObjectId('5def3b1b2a61b9be930647ff'), 'age': 30, 'author': 'Sorabh Pant', 'course': 'Java', 'rating': 4} {'_id': ObjectId('5def3b1b2a61b9be93064800'), 'age': 27, 'author': 'Sahil Shah', 'course': 'jQuery', 'rating': 4}
Here we are deleting a document where author: Joel Smith.
delete_many() method
The delete_many() method is used to delete all the possible documents which will satisfy the query.
result = {'rating': 5} x = courses.delete_many(result) print(x.deleted_count, ' documents deleted.') for i in courses.find(): pprint.pprint(i)
Output
2 documents deleted. {'_id': ObjectId('5dee3c828d178a953eec2ad7'), 'age': 21, 'author': 'Shailesh Bhimanpelli', 'course': 'MongoDB using Python', 'rating': 1} {'_id': ObjectId('5dee451be59a39bf5e67a6fa'), 'age': 23, 'author': 'Ben Thomas', 'course': 'HTML, CSS', 'rating': 4} {'_id': ObjectId('5def3b1b2a61b9be930647ff'), 'age': 30, 'author': 'Sorabh Pant', 'course': 'Java', 'rating': 4} {'_id': ObjectId('5def3b1b2a61b9be93064800'), 'age': 27, 'author': 'Sahil Shah', 'course': 'jQuery', 'rating': 4}
If you want to delete all the documents from the collection then pass an empty query to delete_many() method.
result = mycol.delete_many({}) print(result.deleted_count, " documents deleted.")
Output
4 documents deleted.
drop() method
The drop() method will drop the entire collection from the database.
courses.drop()
This method will drop the courses collection from the CodeSpeedy database.
The entire code with all the methods used in this section is given below.
from pymongo import MongoClient mongo = MongoClient('mongodb://localhost:27017') # CodeSpeedy is the name of the database db = mongo.CodeSpeedy # database for courses offered by CodeSpeedy i.e. courses is the name of the collection courses = db.courses result1 = {'author': 'Joel Smith'} courses.delete_one(result1) result2 = {'rating': 5} result3 = courses.delete_many(result2) print(result3.deleted_count, ' documents deleted.') result4 = mycol.delete_many({}) print(result4.deleted_count, " documents deleted.") # Drops the entire collection from the database courses.drop() for i in courses.find(): print(i)
Leave a Reply