• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:10 mins read
You are currently viewing MongoDB Delete Documents from Collection

You can use deleteOne() or deleteMany() to delete one or multiple documents respectively from the MongoDB collection.

Advertisements

Deleting data is a crucial aspect of database management, and MongoDB offers several methods to accomplish this task. This article will examine various approaches to removing documents from a MongoDB collection. We will cover different deletion operations, including deleteOne, deleteMany, remove, and using the MongoDB administrative command.

First, let’s create a collection with a few documents.


# Create Collection
db.student.insertMany([
      {  
         _id: 1,
         name: "James",
         age: 20,
         gender: "Male",
         marks: 70
 
      },
      {
         _id: 2,
         name: "Elena",
         age: 21,
         gender: "Female",
         marks: 95
      },
      {
         _id: 3,
         name: "Emily",
         age: 20,
         gender: "Female",
         marks: 90
      },
      {
         _id: 4,
         name: "Alice",
         age: 23,
         gender: "Female",
         marks: 75
      },
      {
         _id: 5,
         name: "Alex",
         age: 20,
         gender: "Male",
         marks: 86
      },
      {
         _id: 6,
         name: "Mark",
         age: 23,
         gender: "Male",
         marks: 79
      }
   ]
)

1. Delete a document using the deleteOne() method

The first approach is the deletion of a single document from the collection. For this, the deleteOne() method of MongoDB is used. As the name of the method says, it deletes only a single document even if it finds multiple documents with the criteria.


# Usage of deleteOne() method
db.student.deleteOne({ marks:70 })

The deleteOne() method attempts to delete a document with the field marks equal to 70. If there are multiple documents with marks equal to 70, only one of them will be deleted.

Thus, the execution of the deleteOne query deletes only a single document in the following output.

Delete Methods in MongoDB

More details about this method can be found here.

2. Delete multiple documents using the deleteMany() method

However, if we need to delete all matching documents, we can use the deleteMany() instead of the deleteOne() method. The following query of the deleteMany() method deletes the specific documents from the collection. 


# Usage of deleteMany() method
db.student.deleteMany({ age:20 })

In the above command, the deleteMany() method is invoked to delete all documents that match the specified filter. In this case, the filter specifies that all documents with the field age equal to 20 should be deleted.

Hence the deleteMany() method deletes two documents from the following collection as obtained in the output.

remove documents in MongoDB

3. Delete filtered documents using the deleteMany() method

Additionally, we can use conditional operators in the deleteMany() method to delete certain documents. There, we provided the deleteMany() method query with the $lt operator.


# Usage of conditional operators in the deleteMany() method
db.student.deleteMany({ marks: { $lt:85} })

In the aforementioned query, the deleteMany() method is utilized to delete the particular documents that match the specified criteria. The criteria here is set with the $lt operator to target the documents where the marks field is less than 85.

Here, the output mentions the deletedCount status below, which shows that two documents have been eliminated from the collection.

Delete Methods in MongoDB

4. Remove documents using the remove() method

Contrary to the above approaches for deleting the documents, we have another method called the remove() method. The remove() method in MongoDB is used to delete one or more documents that match the specified query filter.


# Usage of remove() method
db.student.remove({ "_id": 6 })

In the given command, the remove() method is used for removing the document based on the filter criteria. In this case, it targets documents where the _id field is equal to 6.

Hence, the specific document is now removed from the collection according to the status of the output. 

delete documents in MongoDB

5. Remove the document using the runCommand() method

The runCommand() of  MongoDB is also an alternative way to perform the deletion operation. It achieves the same result as the above methods.


# Usage of runCommand() method
db.runCommand(
   {
      delete: "student",
      deletes: [ { q: { _id: 4 }, limit: 1 } ]
   }
)

Here, the runCommand() method is set with the command objects. It specifies the delete operation of the student collection, targeting documents where the _id field is equal to 4. The limit property with a value of 1 ensures that only one matching document will be deleted.

The number of documents removed from the collection was shown in the following output of the aforementioned command.

Delete Methods in MongoDB

6. Delete all documents from the collection

In some cases, we need to delete all the documents permanently from the collection in MongoDB. To do this, we use the deleteMany() method with an empty query filter like given below.


# delete all the documents permanently from the collection
db.student.deleteMany({ })

In the prior query, the deleteMany() method is utilized to delete the entire collection student. The deleteMany() method here passed with the empty query filter {} that matches and deletes all documents in the collection. 

The documents are now deleted from the following specified collection.

Delete Methods in MongoDB

7. Conclusion

In conclusion, by understanding the different deletion options available in MongoDB and applying them appropriately. Moreover, we can effectively manage and maintain the database by removing unnecessary or outdated data.

deleteOne() – delete only one document from the collection

deleteMany() – deletes multiple documents from the collection or all documents from the collection.