MongoDB - Delete Document

delete() method
The remove() method of MongoDB is used to remove a document from a collection. The remove() method takes two parameters. One is the removal criteria, and the other is the justOne flag.
-
Deletion Criteria - (Optional) The deletion criteria according to the documents will be deleted.
-
justOne - (Optional) if set to true or 1, then delete only one document.
Deletion Criteria - (Optional) The deletion criteria according to the documents will be deleted.
justOne - (Optional) if set to true or 1, then delete only one document.
Syntax
The basic syntax of the remove() method is as follows:
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
example
Consider the collection mycol to have the following data.
{ "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title " : "Tutorials Point Overview" }
The following example will delete all documents with the title "Overview of MongoDB".
> db . mycol . remove ({ 'title' : 'MongoDB Overview' }) > db . mycol . find () { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } >
Delete only one
If there are multiple entries and you only want to remove the first entry, then set the justOne parameter in the remove() method .
> db . COLLECTION_NAME . remove ( DELETION_CRITERIA , 1 )
Delete all documents
If you don't specify removal criteria, MongoDB will remove all documents from the collection. This is equivalent to the SQL truncate command.