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