MongoDB Delete

Learn via video courses
Topics Covered

Overview

MongoDB is a popular NoSQL database used in many modern web applications for its flexibility and scalability. MongoDB Delete operation helps us to delete MongoDB documents. We can delete single or multiple documents from a collection, using Delete in MongoDB.

  • For deleting a single document, we use the deleteOne() method.
  • For deleting multiple documents, we use the deleteMany() method.
  • To Delete a Collection in MongoDB we use the drop() method.
  • Delete in MongoDB doesn’t remove indexes, is atomic, and you can specify the level of acknowledgment.

Delete Document in MongoDB

MongoDB Delete operation can be used to delete single or multiple documents from a collection.

Below are three methods used to delete MongoDB documents:

  1. db.collection.deleteOne(): It is used to delete the first document which matches the specified criteria.
  2. db.collection.deleteMany(): It is used to delete all the documents which match the specified criteria.
  3. db.collection.remove(): It is used to delete one or more documents of a collection that match the specified criteria.

Delete Only One Document

The deleteOne() is a MongoDB Delete method used to delete a single document. If multiple documents match the specified filter, then it deletes the first document that matches the criteria.

Syntax:

ParameterTypeDescription
filterdocumentSpecifies criteria to delete MongoDB documents.
writeConcerndocumentIt specifies the level of acknowledgment requested from MongoDB during Delete in MongoDB operation. (Optional)
collationdocumentIt specifies a set of rules which states how strings are compared in MongoDB during the MongoDB Delete operation. (Optional)

Example - 1:

We have a database named movie-api-db and a collection named movies inside it. We have a list of 9 documents inside the movies collection.

movies-collection-inside-movie-api-db

Now, to delete MongoDB document with title = "M3GAN", the command would be:

mongosh-executing-deleteone-method

movies-collection-after-deletion-of-document-using-deleteone-method

Thus using the MongoDB Delete method, the document with title="M3GAN" is deleted, and only 8 documents are left.

Example - 2:

If we have multiple documents with the specified filter, the first document is deleted. We want to delete a document with title="Troll", using Delete in MongoDB.

movies-collection-with-8-documents

mongosh-after-execution-of-deleteone-method

After the MongoDB Delete operation, the first document with _id:"tt11116915" gets deleted. The document with _id:"tt11116917" remains in the database.

movies-collection-where-id-tt11116915-has-been-deleted

Delete All Documents

deleteMany() - is a MongoDB Delete method that is used to delete multiple documents from a collection.

Syntax:

ParameterTypeDescription
filterdocumentSpecifies criteria to delete MongoDB documents. Pass in an empty document ({ }) as a parameter to delete all documents.
writeConcerndocumentIt specifies the level of acknowledgment requested from MongoDB during Delete in MongoDB operation. (Optional)
collationdocumentIt specifies a set of rules which states how strings are compared in MongoDB during the Delete MongoDB operation. (Optional)

Example - 1:

In the collection named “movies”, we have 3 documents with the title=“Strange World”.

movies-collection-with-3-documents-having-title-strange-world

mongosh-after-execution-of-deletemany-method

After the delete MongoDB operation, deletedCount: 3 shows that 3 documents have been deleted.

Example - 2:

In the collection named “movies” we have 6 documents. To delete all documents using the MongoDB Delete operation:

mongosh-after-execution-of-deletemany-without-any-filter

Thus, we can see that all 6 documents have been deleted, using Delete in MongoDB.

movies-collection-after-deletion-of-all-documents

To Delete a Collection in MongoDB, we can use the drop() method.

Delete Behavior in MongoDB

Indexes

Indexes are not deleted during the deletion of the documents using the Delete in MongoDB operation.

Atomic

Deleting a single document is atomic, which means it is entirely removed or not at all.

Acknowledgement

For MongoDB Delete operations, the level of acknowledgment needed from the database after deletion can be specified. It specifies the number of nodes in a replica set that must acknowledge a delete action before it is considered successful. It can be set to any parameters, such as 'w:0' to indicate that the operation will not wait for confirmation from the server, or 'w:1' to indicate that the operation should be acknowledged by the primary node in the replica set. Additional arguments are 'w,' 'w,' and j:'true'.

MongoDB remove() Method

The remove() method is used to remove documents from a collection.

Syntax:

ParameterTypeDescription
querydocumentSpecifies criteria to delete MongoDB documents.
justOnebooleanIf the MongoDB Delete operation needs to be limited to a single document, this parameter should be set to true. (Optional)
writeConcerndocumentIt specifies the level of acknowledgment requested from MongoDB during Delete in MongoDB operation. (Optional)
collationdocumentIt specifies a set of rules which states how strings are compared in MongoDB during the Delete MongoDB operation. (Optional)
letdocumentA list of variables is specified. (Optional)

Example - 1:

The collection named movies has 6 documents. We want to remove the movie with releaseDate:"2009-12-15".

movies-collection-with-6-documents

mongosh-after-execution-of-remove-method

deletedCount:1, which means that the document matching the specified filter has been deleted.

Example - 2:

We can also remove all the documents present in the collection using the remove() method.

movies-collection-with-4-documents

Syntax:

mongosh-after-execution-of-remove-without-filter

movies-collection-after-removing-all-documents

Mongosh -> deletedCount: 4. Therefore, all 4 documents are deleted.

Deprecation Warning!

mongosh-after-execution-of-the-remove-method

In the above image we can see a message:

The remove() method has been deprecated so that deleteOne() and deleteMany() methods are used to delete in MongoDB. This is by the MongoDB CRUD specification, which provides a uniform API for all MongoDB CRUD operations including the MongoDB Delete operation.

FAQs

Q. Can I Delete in MongoDB based on multiple criteria?

A. Yes, it can be done by passing a filter that has multiple criteria.

Syntax:

Q. Can a MongoDB Delete operation be undone?

A. MongoDB does not have an inbuilt method to undo a delete operation.

Q. Can I Delete a Collection in MongoDB?

A. Yes, we can Delete a Collection in MongoDB using the drop() method.

Syntax:

ParameterTypeDescription
writeConcerndocumentIt specifies the level of acknowledgment requested from MongoDB during Delete in MongoDB operation. (Optional)

Conclusion

  • deleteOne():
    It is a MongoDB Delete method to delete the first document in a collection with the specified filter.
  • deleteMany():
    It is a MongoDB Delete method to delete all the documents in a collection that match the specified filter.
  • remove():
    It is used to delete MongoDB documents in a collection, but it has been deprecated to favor the usage of deleteOne() and deleteMany().
  • drop():
    It is used to Delete a Collection in MongoDB.
  • Delete in MongoDB is atomic, doesn’t remove indexes, and the level of acknowledgment can be specified.