3

Python MongoDB - find_one_and_delete Query

 3 years ago
source link: https://www.geeksforgeeks.org/python-mongodb-find_one_and_delete-query/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK