The delete_one function deletes a single document from the MongoDB collection. Previously we had inserted the document for a user named Mike. Let’s have a look at the MongoDB document inserted:
table.find_one({'name':'Mike'})
We will now delete this MongoDB document:
table.delete_one({'name':'Mike'})
Let’s try to fetch this document after deletion. If this document is not available in our MongoDB collection, the find_one function will return nothing.
table.find_one({'name':'Mike'})
Output: Nothing is returned. Since we get nothing in return, it means that the MongoDB document doesn’t exist anymore. As we saw that the insert_many function is used to insert multiple documents in MongoDB collection, delete_many is used to delete multiple documents at once. Let’s try to delete two MongoDB documents by the name field: myquery = ({ "name":{"$in": ["Gyan","Eliot"]}}) x = table.delete_many(myquery) print(x.deleted_count, " documents deleted.") Here, the deleted count stores the number of deleted MongoDB documents during the operation. The ‘$in’ is an operator in MongoDB. |
0 comments:
Post a Comment