December 2015
Beginner to intermediate
202 pages
4h
English
Sometimes, you need to delete a single record, while at other times you need to delete many. This recipe shows you how to delete multiple records matching a filter.
As before, determine the records that you want to delete, and create a filter for them.
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.pythonbicookbook
customers = db.customers
result = customers.delete_many({
'contact_requested': False })
print(result.deleted_count)Given a filter, delete_many() deletes all the records matching the filter. We then print out the number of records deleted.
Read now
Unlock full access