In MongoDB, the db.colloction.remove() method is used to delete documents from a collection. The remove() method works on two parameters.
1. Deletion criteria: With the use of its syntax you can remove the documents from the collection.
2. JustOne: It removes only one document when set to true or 1.
Syntax:
db.collection_name.remove (DELETION_CRITERIA)
If you want to remove all documents from a collection, pass an empty query document {} to the remove() method. The remove() method does not remove the indexes.
Let's take an example to demonstrate the remove() method. In this example, we remove all documents from the "rookienerd" collection.
db.rookienerd.remove({})
If you want to remove a document that match a specific condition, call the remove() method with the <query> parameter.
The following example will remove all documents from the rookienerd collection where the type field is equal to programming language.
db.rookienerd.remove( { type : "programming language" } )
If you want to remove a single document that match a specific condition, call the remove() method with justOne parameter set to true or 1.
The following example will remove a single document from the rookienerd collection where the type field is equal to programming language.
db.rookienerd.remove( { type : "programming language" }, 1 )