A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-mongodb-find_one_and_delete-query/ below:

Python MongoDB - find_one_and_delete Query

Python MongoDB - find_one_and_delete Query

Last Updated : 15 Jul, 2025

MongoDB

is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs.

Find_one_and_delete Query

This function is used to delete a single document from the collection based on the filter that we pass and returns the deleted document from the collection. It finds the first matching field that matches the filter and deletes it from the collection i.e finds a single document and deletes it, returning the document.

Syntax: Collection.find_one_and_delete(filter, projection=None, sort=None, session=None, **kwargs) Parameters:
Example 1: Sample Database: Python3 1==
# importing Mongoclient from pymongo
from pymongo import MongoClient 


# Making Connection
myclient = MongoClient("mongodb://localhost:27017/") 

# database 
db = myclient["mydatabase"]

# Created or Switched to collection 
# names: GeeksForGeeks
Collection = db["GeeksForGeeks"]

# Defining the filter that we want to use.
Filter ={'Manufacturer': 'Maruti'}

# Using find_one_and_delete() function.
print("The returned document is:")
print(Collection.find_one_and_delete(Filter,
                                     projection = None,
                                     sort = None))

# Printing the data in the collection
# after find_one_and_delete() operation.
print("\nThe data after find_one_and_delete() operation is:")

for data in Collection.find():
    print(data)
Output: Example 2: Python3 1==
# importing Mongoclient from pymongo
from pymongo import MongoClient 


# Making Connection
myclient = MongoClient("mongodb://localhost:27017/") 

# database 
db = myclient["mydatabase"]

# Created or Switched to collection 
# names: GeeksForGeeks
Collection = db["GeeksForGeeks"]

# Defining the filter that we want to use.
Filter ={'Manufacturer': 'Hyundai'}

# Using find_one_and_delete() function.
print("The returned document is:")
print(Collection.find_one_and_delete(Filter,
                                     projection = None,
                                     sort = None))

# Printing the data in the collection
# after find_one_and_delete() operation.
print("\nThe data after find_one_and_delete() operation is:")

for data in Collection.find():
    print(data)
Output:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4