Deleting multiple records using PyMongo

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.

Getting ready

As before, determine the records that you want to delete, and create a filter for them.

How to do it…

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)

How it works…

Given a filter, delete_many() deletes all the records matching the filter. We then print out the number of records deleted.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset