MongoDB: Delete Document

CRUD in MongoDB stands for Create, Read, Update and Delete. These are some of the most important operations for any Database. In this article, we are going to see how can we delete documents present inside a collection in MongoDB.
Deletion of documents MongoDB can be done in two ways:

  • deleteOne()
  • deleteMany()

You are going to see deleteOne() in this article. deleteMany() part of the delete document, you are going to see in the further part of this course.

deleteOne()

Syntax

deleteOne(filter,options)

deleteOne() Command is used to delete the first document that satisfies the parameters. DeleteOne() contains two parameters:

1) filter: Well, in this part of the article we are going to discuss a filter parameter. Let's understand this concept with the help of an example:

  • Example: You have a collection named edatabase and that collection contains the following documents:
MONGODB

Now moving ahead you want to delete documents that contain a Salary of 30000. This is a part, where the use of filter parameter come into consideration:

MONGODB

You can notice that 1 document from the collection is deleted. To see the collection after deletion of the document follow the code below:

MONGODB

NOTE: In easy language, you can consider filter as a condition according to which the documents will be deleted. 

2) Options: this Parameter consists of 3 types.

  • writeConcern
  • Collation
  • hint

These three types were are going to discussing in detail in the further part of this course.