The MongoDB remove () function is used to remove data from the collection.
The update () function can be used for MONGODB data updates. It is a good practice to execute the Find () command before executing the Remove () function to determine if 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 the 2.6 version, the syntax format is as follows:
Db.collection.remove (
<query>,
{
justone: <boolean>,
Writeconcern: <document>
}
)
Parameter description:
query: (optional) The condition of the deleted file.
Justone: (optional) If set to TRUE or 1, only one document is deleted
Writeconcern: (optional) the level at which the exception is thrown.
Example
We perform two insertions in the following documents:
>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 the data:
> Db.col.find ()
{"_id": ObjectId ("56066169ade2f21f36b03137"), "title": "MongoDB Tutorial", "description": "MongoDB is a 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 Tilte as "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 first found record you can set Justone to 1 as follows:
>db. Collection_name.remove (deletion_criteria,1)
If you want to delete all data, you can use the following methods:
>db.col.remove ({})
>db.col.find ()
>
List of Notes
The Remove () method is obsolete and is now officially recommended using the Deleteone () and Deletemany () methods.
If you delete all the documents under the collection:
Db.inventory.deleteMany ({})
Delete all documents with status equal to a: