Home >>MongoDB Tutorial >MongoDB Delete Document
The remove() Method
To remove a document from the collection, MongoDB's remove() method is used. Two parameters are recognized by the remove() method. One is the criteria for deletion and the second is the flag JustOne.
Syntax
Basic syntax of remove() method is as follows −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Consider the mycol collection has the following data.
{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"}, {_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"}, {_id : ObjectId("507f191e810c19729de860e3"), title: "phptpoint Ove
The example below remove all documents with the title 'MongoDB Overview'.
>db.mycol.remove({'title':'MongoDB Overview'}) WriteResult({"nRemoved" : 1}) > db.mycol.find() {"_id" : ObjectId("507f191e810c19729de860e2"), "title" : "NoSQL Overview" } {"_id" : ObjectId("507f191e810c19729de860e3"), "title" : "phptpoint Overview" }
Remove Only One
If multiple records exist and you want to delete only the first record, set the parameter to justOne in the remove() method.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
If you do not define the deletion criteria, then MongoDB delete the whole set of documents. This is similar to the Truncate command from SQL.
> db.mycol.remove({}) WriteResult({ "nRemoved" : 2 }) > db.mycol.find() >