December 2015
Beginner to intermediate
202 pages
4h
English
Sometimes, you just need to delete a record. That's easy with the delete_one() method.
As we've seen in the previous recipes, the first thing to do is to create a filter to find the record to delete.
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.pythonbicookbook
customers = db.customers
result = customers.delete_one({
'first_name': 'Bob',
'last_name': 'Smith' })
print(result.deleted_count)Given a filter, delete_one() deletes the first record that matches the filter. We then print out the count of records deleted to ensure that only one record was deleted.
Read now
Unlock full access