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 document.
- justone : (optional) If set to TRUE or 1, only one document is deleted.
- Writeconcern : (optional) the level at which the exception is thrown.
Instance
We perform two insertions in the following documents:
>Db.Col.Insert({Title: ' MongoDB Tutorial ', Description: MongoDB is a Nosql database ' , by: Rookie tutorial ' , Tags: [ ' MongoDB ' , ' database ' , "NoSQL" ], Likes:< Span class= "PLN" > 100})
Use the Find () function to query the data:
>Db.Ao..Find(){ "_ID" : ObjectId("56066169ade2f21f36b03137"), "Title" : "MongoDB Tutorial", "description" : "MongoDB is a Nosql database", "By" : "Beginner's 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 with the 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 first found record 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 (like the General SQL truncate command):
>db. Col. Remove({})>db. Col. Find()>
MongoDB Delete Document