Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python MongoDB - find_one Query
Next article icon
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:
  • 'filter' : A query that matches the document to delete.
  • 'projection' (optional): A list of field names that should be returned in the result document or a mapping specifying the fields to include or exclude. If 'projection' is a list "_id" will always be returned. Use a mapping to exclude fields from the result (e.g. projection={'_id': False}).
  • 'sort' (optional): A list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is deleted.
  • 'session' (optional): A class: "~pymongo.client_session.ClientSession".
  • '**kwargs' (optional): Additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).
Example 1:Sample Database:Python3 1==
# importing Mongoclient from pymongofrompymongoimportMongoClient# Making Connectionmyclient=MongoClient("mongodb://localhost:27017/")# databasedb=myclient["mydatabase"]# Created or Switched to collection# names: GeeksForGeeksCollection=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:")fordatainCollection.find():print(data)
Output:Example 2:Python3 1==
# importing Mongoclient from pymongofrompymongoimportMongoClient# Making Connectionmyclient=MongoClient("mongodb://localhost:27017/")# databasedb=myclient["mydatabase"]# Created or Switched to collection# names: GeeksForGeeksCollection=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:")fordatainCollection.find():print(data)
Output:

Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp