![]() |
VOOZH | about |
find_one_and_delete() method in PyMongo is used to find a single document, delete it and return the deleted document. Itβs useful when you need to both remove and retrieve a document in one operation. A filter is provided to match the document and optionally a sort condition to decide which document to delete if multiple match.
collection.find_one_and_delete(filter, options)
Parameters:
Let's see some Examples to understand it better.
This code shows how to use find_one_and_delete() to remove a document where the Manufacturer is "Apple" from the collection.
Output :
Explanation: find_one_and_delete() finds the first document where "Manufacturer" is "Apple" and deletes it from the collection.
In this Example a document with "Manufacturer" set to "Redmi" is removed from the collection using find_one_and_delete() method.
Output :
Explanation: find_one_and_delete() searches for the first document where "Manufacturer" is "Redmi" and deletes it.
Related Articles: