11.5. Deleting documents in MongoDB: Deleting documents with conditions

Página 59

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() 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:

6011.6. Deleting documents in MongoDB: Deleting entire collections

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text