Sunday, October 18, 2020

Mongo DB - Deleting Documents from Collections

In this article we will see how to delete data in Mongo DB collections.

For deleting a single record, the following delete query can be used. Note that this query has one part inside the remove() method. This part is for filtering the documents that have to be deleted. The following query will delete the only that particular book with the specified object ID.

db.Books.remove({_id: ObjectId("1234d4ef422drf433rffer4d2445f43")});

For deleting multiple records, the following delete query can be used. Note that this query also has only one part inside the delete() method. This part is for filtering the documents that have to be updated. The following delete query will delete all the books whose author is 'Anonymous'.

db.Books.remove({author: "Anonymous"});

Hope you find this crisp and useful.

Cheers!

No comments:

Post a Comment