3 common usages of the MongoDB remove function _mongodb

Source: Internet
Author: User
Tags mongodb

In MongoDB, the Db.collection.remove () method is used to delete documents from the document. You can delete all documents from a collection, delete all documents that match one condition, or restrict actions to delete only one document.

Delete all documents:

To delete all documents in the collection, you need to pass an empty query document {} to the Remove () method. The Remove () method does not delete the index. Here is an example of removing all documents from the inventory collection:

Copy Code code as follows:
Db.inventory.remove ({})

Delete all documents in the collection, the drop () method may be more efficient, and the drop () method deletes the entire collection, including the index, and then recreate the collection and index.

To delete a document that matches a criterion:

Deleting a document that matches a delete condition requires calling the Remove () method and passing a <query> parameter. The following example deletes all documents of type field value food from the inventory collection:

Copy Code code as follows:
Db.inventory.remove ({type: "Food"})

For large deletions, it is possible to copy the document that needs to be retained into a new collection and then delete the collection using the Drop () method on the original collection.

To delete a document that matches a criterion:

Deletes a document, calls remove (), sets the value of the Justone parameter to TRUE or 1. The following is an example of a document that deletes a Type field value of food from the inventory collection:

Copy Code code as follows:
Db.inventory.remove ({type: "Food"}, 1)

Deletes a document in a specific order using the Findandmodify () method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.