11.5. Deleting documents in MongoDB: Deleting documents with conditions
Page 59 | Listen in audio
11.5. Deleting Documents in MongoDB: Deleting Documents with Conditions
One of the most critical operations in any database management system is document deletion. In MongoDB, this is no different. Deleting documents is a task that must be carried out very carefully as once a document is deleted it cannot be recovered. In this section, we will discuss how to delete documents in MongoDB with conditions.
MongoDB provides two methods for deleting documents from a collection: deleteOne()
and deleteMany()
. Both methods accept a condition object as an argument. This condition object is similar to the one we use in the search operation. It specifies the conditions that documents must meet to be deleted.
Deleting Documents with Conditions using deleteOne()
The deleteOne()
method deletes the first document that matches the specified condition. For example, if we want to delete a document whose 'name' field is 'John', we can do it as follows:
db.collection('users').deleteOne({ name: 'John' });
This command will delete the first document in the 'users' collection where the 'name' field is 'John'. If there are multiple documents that meet this condition, only the first one will be deleted.
Deleting Documents with Conditions using deleteMany()
The deleteMany()
method deletes all documents that match the specified condition. For example, if we want to exclude all documents whose 'age' field is less than 18, we can do this as follows:
db.collection('users').deleteMany({ age: { $lt: 18 } });
This command will delete all documents in the 'users' collection where the 'age' field is less than 18. Unlike the deleteOne()
method, the deleteMany() method code> does not stop after deleting the first document that meets the condition. It continues to cycle through the collection until all documents that meet the condition are deleted.
Deletion of Documents with Complex Conditions
The conditions for deleting documents do not need to be simple. We can specify complex conditions using logical and comparison operators. For example, if we want to exclude all documents whose 'age' field is less than 18 and whose 'country' field is 'USA', we can do this as follows:
db.collection('users').deleteMany({ age: { $lt: 18 }, country: 'USA' });
This command will delete all documents in the 'users' collection where the 'age' field is less than 18 and the 'country' field is 'USA'. Conditions are evaluated together, meaning a document must meet all conditions to be deleted.
Conclusion
Deleting documents is a critical operation that must be performed with care. In MongoDB, we can delete documents using the deleteOne()
and deleteMany()
methods, which accept a condition object as an argument. Conditions can be simple or complex, depending on the needs of our application.
Now answer the exercise about the content:
What is the difference between deleteOne() and deleteMany() methods in MongoDB?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: