Delete one or more documents from a MongoDB collection using PyMongo
One or more documents from MongoDB can be deleted using the methods collection.delete_one() and collection.delete_many() from the PyMongo API, by specifying a filter condition.
A MongoDB collection consists of several JSON documents in binary format.
To delete records from a MongoDB collection, the PyMongo Python driver offers several variants of delete operation through the methods
delete_one()
delete_many()
Delete one document satisfying a filter condition
To delete only one document, that meets the specified filter conditionamong several others, the method can be called on a object.
The example below specifies the condition for the single document that is to be deleted is {"red" : 123, "green" : 223}.
Example
python
# import the python driver for MongoDB
from pymongo import MongoClient
# Create a MongoDB client
mongoDBConnection = MongoClient('mongodb://localhost:27017/')
# Get a collection instance
database = mongoDBConnection.sample
collection = database.sample1
# Print the collection before deleting a single record meeting the criteria
print("Before deletion:");
for mongoDBdocument in collection.find():
print(mongoDBdocument)
# Delete one record satisfying the filter condition
deleteResult = collection.delete_one({"red" : 123, "green" : 223})
print("Number of documents deleted from the MongoDB collection:")
print(deleteResult.deleted_count)
# Print the collection after deleting a single record meeting the criteria
print("After deletion:");
for mongoDBdocument in collection.find():
print(mongoDBdocument)
Output
Output
Before deletion:
{'_id': ObjectId('5ab9fad002334a0303b029c1'), 'color1': 'red', 'color2': 'blue'}
{'_id': ObjectId('5ab9fb8302334a031cc2fc14'), 'red': 146, 'green': 46, 'blue': 246}
{'_id': ObjectId('5aba309f74d9af6fff2f2209'), 'red': 123.0, 'green': 223.0, 'blue': 23.0}
Number of documents deleted from the MongoDB collection:
1
After deletion:
{'_id': ObjectId('5ab9fad002334a0303b029c1'), 'color1': 'red', 'color2': 'blue'}
{'_id': ObjectId('5ab9fb8302334a031cc2fc14'), 'red': 146, 'green': 46, 'blue': 246}
Deleting multiple documents satisfying a filter condition
To delete multiple documents from a MongoDB collection the db.collection.deletemany()method can be used.
db.collection.deletemany()can be passed with a JSON document specifying the filter condition. This method removes all the documents matching the specified filter condition.
If the filter condition is empty the method db.collection.deletemany()removes all the documents from the document collection.
Example
python
# import the python driver for MongoDB
from pymongo import MongoClient
# Create a MongoDB client
mongoDBConnection = MongoClient('localhost', 27017)
# Get a collection instance
collection = mongoDBConnection.sample.orders
# Print the collection before deleting a single record meeting the criteria
print("Before deletion:");
for mongoDBdocument in collection.find():
print(mongoDBdocument)
# Delete all the order records which are in cancelled state
deleteResult = collection.delete_many({"Status" : "Cancelled"})
print("Number of documents deleted from the MongoDB collection:")
print(deleteResult.deleted_count)
# Print the collection after deleting a single record meeting the criteria
print("After deletion:");
for mongoDBdocument in collection.find():
print(mongoDBdocument)
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on