Delete Documents in MongoDB

After creating an updating the document, a situation might occur where you want to delete any document or a set of documents. MongoDB also allows you to delete any particular document or multiple collections of documents. In this chapter, you will learn about how to delete documents from a MongoDB database.

Deleting Documents in MongoDB

MongoDB allows you to delete a document or documents collectively using its one of the three methods. Three methods provided by MongoDB for deleting documents are:
  1. db.collection.deleteOne()
  2. db.collection.remove()
  3. db.collection.deleteMany()

db.collection.deleteOne() Method

This method is used to delete only a single document, even when more than one document matches with the criteria. Here is an example of using this db.collection.deleteOne() method for deleting the single document. To perform this process here, we have created a database and saved all the data separately.

Example:
db.programmers.insert(
[
  { name: "James Gosling" },
  { name: "Dennis Ritchie" },
  { name: "Bjarne Stroustrup" }
]
)

Once the insertion process is done, you can run the query (mentioned below) to return multiple results:

Example:
db.programmers.find()
Output:

Once you execute the above line, you will find that some documents match your query criteria and will get displayed as output. Now, you can make use of the following criteria to delete the documents.

Example:
db.programmers.deleteOne( { name: { $in: [ "Dennis Ritchie", "Bjarne Stroustrup"] } } )

Executing this statement, you will notice that, although two documents match the criteria, only one document gets deleted.

Deletes Each Document

It is possible for you to delete all your existing documents in a collection by simply excluding the filter criteria that is mentioned in the parenthesis () and specifying the document names within it. Now, to delete all documents from the programmers' collection, you have to write the query like this:

Example:
db.programmers.remove( {} )
Output:

But, when the filtering is done for removing elements, the db.collection.remove() method will document which matches the specified criteria.

Here, we delete all documents where the artist name is "James Gosling".

Example:
db.programmers.remove( { name: "James Gosling" } )
Output:

db.collection.deleteMany() Method

MongoDB allows you to delete multiple documents using the db.collection.deleteMany() method. This method deletes all your documents whichever match its criteria mentioned in the parameter. To check its implementation of db.collection.deleteMany() method, you can use the method the same way as done previously:

Example:
db.programmers.deleteMany( { name: { $in: [ "Dennis Ritchie", "Bjarne Stroustrup" ] } } )
Output:

In this way, continuing from the previous example implementation, now all the records were also deleted, and the deleteMany() method was used this time. Now, if you try to find the documents using find() method, it won't show any result of your query or search.


Scroll Back to Top