Chuck's Academy

Database

DELETE Operations in MongoDB

In this chapter, we will learn how to perform DELETE operations in MongoDB, which allow us to remove documents from a collection. MongoDB offers several methods to delete documents, either individually or in bulk, depending on the search criteria we specify.

Deleting a Document with deleteOne()

The deleteOne() method deletes the first document that matches the search criteria. If we find multiple documents that meet the criteria, only the first one will be deleted.

Example of deleteOne()

javascript
"In this example, we are deleting the document from the users collection where the _id field is equal to one."

This command will only delete the first document that matches the search criteria, even if there are several that meet the same criteria.

Deleting Multiple Documents with deleteMany()

If we need to delete multiple documents that match a criterion, we can use deleteMany(). This method deletes all documents that meet the criteria.

Example of deleteMany()

javascript
"This command deletes all documents from the users collection where the age is less than 25, using the $lt operator."

Conditional Deletion

As with other operations in MongoDB, we can perform deletions based on complex conditions using logical operators like $and, $or, and $not. For example, if we wanted to delete users who are over 30 years old or have not verified their email, we could use the following command:

javascript
"Here we are deleting all users who are over 30 years old or have not verified their email, using the $or operator."

Example with $and

We can combine conditions with $and to delete documents that meet several conditions simultaneously:

javascript
"In this example, we are deleting the documents where the age is less than 20 and the is_active field is false, using the $and operator."

Caution When Using deleteMany()

It is important to be careful when using deleteMany() to ensure that we are not deleting more documents than we really want. If we omit the search clause, MongoDB will delete all documents in the collection.

javascript
"This command deletes all documents from the users collection. It is important to use deleteMany with care, especially when leaving the search clause empty."

Alternative: Soft Delete

Instead of permanently deleting documents, some applications prefer to implement a technique called soft delete, which involves marking a document as deleted without actually removing it. This allows the document to remain in the database but be excluded from queries.

Example of Soft Delete

javascript
"In this example, we are marking the document with _id equal to two as deleted, adding a deleted_at field with the current date."

The advantage of this technique is that we can "restore" the deleted documents simply by removing or updating the deleted_at field.

Summary

In this chapter, we have learned how to delete documents in MongoDB using the deleteOne() and deleteMany() methods. We also explored how to use logical operators in deletions and how to implement the soft delete approach to retain data without permanently deleting it. In the next chapter, we will learn how to manage security and permissions in MongoDB.


Ask me anything