Contents
Python MongoDB Delete Documents
You can delete one or more documents from a collection using delete_one() or delete_many() functions respectively.
Delete One Document
delete_one() function can be called on a collection. The function takes query as an argument and deletes only the first and single document that matches the given query.
PyMongo Example to delete a single document
In the following example, we will delete a single document that matches the given criteria.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
#use database named "organisation"
mydb = myclient["organisation"]
#use collection named "developers"
mycol = mydb["developers"]
print('Documents in Collection\n-----------------------')
for doc in mycol.find():
print(doc)
#query to delete document
query = {'address':'India'}
#delete one document
mycol.delete_one(query)
print('\nDocuments in Collection after delete_one()\n-----------------------')
for doc in mycol.find():
print(doc)
Run 
Delete many documents
delete_many() function can be called on a collection. The function takes query as an argument and deletes all the documents that matches the given query.
PyMongo Example to delete multiple documents
In the following example, we will delete multiple documents that match the given criteria.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
#use database named "organisation"
mydb = myclient["organisation"]
#use collection named "developers"
mycol = mydb["developers"]
print('Documents in Collection\n-----------------------')
for doc in mycol.find():
print(doc)
#query to delete document
query = {'address':'India'}
#delete many document
mycol.delete_many(query)
print('\nDocuments in Collection after delete_many ()\n-----------------------')
for doc in mycol.find():
print(doc)
Run 