• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

How to delete document in MongoDB

Remove Documents

In MongoDB, the db.collection.remove() method removes documents from a collection. You can remove all documents from a collection, remove all documents that match a condition, or limit the operation to remove just a single document.

This tutorial provides examples of remove operations using the db.collection.remove() method in the mongo shell.

Remove All Documents

To remove all documents from a collection, pass an empty query document {} to the remove() method. The remove() method does not remove the indexes.

The following example removes all documents from the inventory collection:

Screenshot_44

Remove Documents that Match a Condition

To remove the documents that match a deletion criteria, call the remove()method with the <query>parameter.

Aspired to become an Mongo DB Training?
Explore the post to discover the know-hows on Mongo DB.

The following example removes all documents from the inventory collection where the type field equalsfood:

Screenshot_45

For large deletion operations, it may be more efficient to copy the documents that you want to keep to a new collection and then use drop()on the original collection.

Remove a Single Document that Matches a Condition

To remove a single document, call the remove() method with the justOne parameter set to true or 1.

The following example removes one document from the inventory collection where the type field equalsfood:

Screenshot_47

To delete a single document sorted by some specified order, use the find and modify method.

For indepth understanding click on

Summary
Review Date
Reviewed Item
How to delete document in MongoDB
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on How to delete document in MongoDB"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.