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.
Get Python Business Intelligence Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.