1. Home
  2. MongoDB
  3. MongoDB Tutorial
  4. MongoDB – Delete Document

MongoDB – Delete Document

MongoDB Delete Document

Use db.colloction.remove() method to delete any document from MongoDB collection on basis of critaria.

Syntax:

> db.colloction.remove(CONDITION)

Delete Matching Document

Now use the following command deletes a specific document from MongoDB collection with user_name = rahul.

> db.users.remove({"user_name":"rahul"})

The above command will remove all document having user_name rahul. To remove only first matching document from collection use following command.

> db.users.remove({"user_name":"rahul"},1)

Delete All Documents in Collection

Use the following command with no deletion criteria to delete all documents from the specific collection. For example, below will delete all documents from users collection.

> db.users.remove()
Tags ,