MongoDB Delete Document _mongodb

Source: Internet
Author: User
Tags mongodb mongodb collection mongodb tutorial

In the previous chapters we have learned how to add data and update data for a collection in MongoDB. In this section we will continue to learn the deletion of the MongoDB collection.

The MongoDB remove () function is used to remove data from the collection.

MongoDB data updates can use the update () function. It is a good practice to perform the find () command before performing the Remove () function to determine whether the condition of execution is correct. Grammar

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

Db.collection.remove (
   <query>
   <justOne>
)

If your MongoDB is after version 2.6, the syntax format is as follows:

Db.collection.remove (
   <query>,
   {
     justone: <boolean>,
     Writeconcern: <document>
   }
)

Parameter description: query: (optional) The conditions for the deleted document. Justone: (optional) If set to TRUE or 1, only one document will be deleted. Writeconcern: (optional) the level at which the exception is thrown. Instance

We performed two inserts in the following document:

>db.col.insert ({title: ' MongoDB Tutorial ', 
    Description: ' MongoDB is a Nosql database ', by
    : ' Rookie tutorial ',
    URL: ' http:// Www.runoob.com ',
    Tags: [' mongodb ', ' database ', ' NoSQL '],
    likes:100
})

Use the Find () function to query for data:

> Db.col.find ()
{"_id": ObjectId ("56066169ade2f21f36b03137"), "title": "MongoDB Tutorial", "description": "MongoDB is a Nosql database ", by": "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["MongoDB", "Database", "Nosql"], "likes": 100}
{"_id": ObjectId ("5606616dade2f21f36b03138"), "title": "MongoDB Tutorial", "description": "MongoDB is a Nosql database", "by" : "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["MongoDB", "Database", "NoSQL"], "likes": 100}

Next we remove the document title "MongoDB Tutorial":

>db.col.remove ({' title ': ' MongoDB Tutorial '})
Writeresult ({"Nremoved": 2})           # deleted two data
>db.col.find () .....                                        # no data

If you only want to delete the record found in the first one, you can set Justone to 1, as follows:

>db. Collection_name.remove (deletion_criteria,1)

If you want to delete all the data, you can use the following methods (similar to the TRUNCATE command for regular SQL):

>db.col.remove ({})
>db.col.find ()
>
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.