MongoDB Delete Document

Source: Internet
Author: User

Remove () method

The Remove () method in MongoDB is used to remove a document from the collection. The Remove () method accepts two parameters. One is a delete condition, the second is a flag: Justone.
Criteria-(optional) The collection that meets the deletion criteria will be deleted.
Justone-(optional) If set to TRUE or 1, only one document is deleted.

Grammar

The basic syntax for the Remove () method is as follows-

>db. Collection_name.remove (Delletion_critteria)


Assume that the MyCol collection has the following data.

> Db.mycol.find ({}, {' _id ': 1, ' title ': 1})
{"_id": 101, "title": "MongoDB Guide"}
{"_id": 102, "title": "NoSQL Database"}
{"_id": 104, "title": "Python Quick Guide"}
{"_id": "" title ":" Update by Save () Method. "}



The following example deletes a document that _id to "100".

> Db.mycol.find ({}, {' _id ': 1, ' title ': 1})
{"_id": 101, "title": "MongoDB Guide"}
{"_id": 102, "title": "NoSQL Database"}
{"_id": 104, "title": "Python Quick Guide"}
{"_id": "" title ":" Update by Save () Method. "}
>
> db.mycol.remove ({' _id ': 100})
Writeresult ({"nremoved": 1})
> Db.mycol.find ({}, {' _id ': 1, ' title ': 1})
{"_id": 101, "title": "MongoDB Guide"}
{"_id": 102, "title": "NoSQL Database"}
{"_id": 104, "title": "Python Quick Guide"}



Delete only one document record

If you have more than one record, and you want to delete only the first record, set the Justone parameter in the Remove () method.

>db. Collection_name.remove (deletion_criteria,1)


Delete all document Records

To delete all document records, set the Justone parameter in the Remove () method.

If you do not specify a delete condition, MongoDB deletes all documents in the collection. This is equivalent to the SQL TRUNCATE command.

>db.mycol.remove ()
>db.mycol.find ()

MongoDB Delete Document

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.