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.


> db.colloction.remove(CONDITION)

Delete Matching Document

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

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

The above command will remove all documents having user_name rahul. To remove only the first matching document from the collection use the 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 user's collections.

> db.users.remove()

Post a Comment