VOOZH about

URL: https://www.geeksforgeeks.org/python/python-mongoddb-find_one_and_delete-query/

⇱ Python MongoDB - find_one_and_delete query - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python MongoDB - find_one_and_delete query

Last Updated : 2 Jul, 2025

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.

Syntax 

collection.find_one_and_delete(filter, options)

Parameters:

  • filter (dict): a query that matches the document to delete.
  • projection (dict, optional): specifies which fields to return in the deleted document.
  • sort (list of tuples, optional): determines which document to delete if multiple match. Example: [("age", 1)] for ascending sort.
  • hint (optional): specifies the index to use for the query. 

Let's see some Examples to understand it better.

Sample Collection used in this article:

πŸ‘ sample-collection
Collection use for find_one_and_delete Query

Example 1:

This code shows how to use find_one_and_delete() to remove a document where the Manufacturer is "Apple" from the collection.

Output :

πŸ‘ findanddelete_output
Snapshot of Terminal showing output of find_one_and_delete Query

Explanation: find_one_and_delete() finds the first document where "Manufacturer" is "Apple" and deletes it from the collection.

Example 2:

In this Example a document with "Manufacturer" set to "Redmi" is removed from the collection using find_one_and_delete() method.

Output :

πŸ‘ delete-output-2
Snapshot of Terminal showing output of find_one_and_delete Query

Explanation: find_one_and_delete() searches for the first document where "Manufacturer" is "Redmi" and deletes it.

Related Articles:

Comment
Article Tags: