Article image Deleting documents in MongoDB

11. Deleting documents in MongoDB

Page 54 | Listen in audio

Deleting documents in a MongoDB database is an essential task that every developer must master. In this chapter, we'll explore how to delete documents in MongoDB, from basic concepts to more advanced techniques. This chapter is crucial to understanding how to effectively manage your data in MongoDB.

Basic concepts

First of all, it is important to understand that deleting documents in MongoDB is an irreversible operation. Once a document is deleted, it cannot be recovered. Therefore, it is essential to be careful when deleting documents and ensure that you have adequate backups of your data.

There are two main functions you can use to delete documents in MongoDB: deleteOne() and deleteMany(). As the names suggest, deleteOne() is used to delete a single document that matches the specified criteria, while deleteMany() is used to delete all documents that match the specified criteria.

Using deleteOne()

To use the deleteOne() function, you need to specify a filter that matches the document you want to delete. For example, if you want to delete a document with the name "John", you can use the following command:

db.collection.deleteOne({ name: "John" })

If there are multiple documents with the name "John", only the first document found will be deleted.

Using deleteMany()

The deleteMany() function is used in a similar way to the deleteOne() function, but it deletes all documents that match the specified filter. For example, to delete all documents with the name "John", you can use the following command:

db.collection.deleteMany({ name: "John" })

This will delete all documents that have "John" as the value for the name field.

Deleting all documents in a collection

If you want to delete all documents in a collection, you can use the deleteMany() function without specifying any filters. For example, the following command deletes all documents in the collection:

db.collection.deleteMany({})

Again, remember that deleting documents is an irreversible operation, so use this command with caution.

Conditional deletion of documents

You can also delete documents based on conditions. For example, you may want to delete all documents where the age is greater than 30. To do this, you can use the following command:

db.collection.deleteMany({ age: { $gt: 30 } })

This will delete all documents where the age field value is greater than 30.

Conclusion

Deleting documents in MongoDB is a simple task, but it is important to understand the basic concepts and use the correct function for each situation. Always remember that deleting documents is an irreversible operation, so always exercise caution when deleting documents and ensure that you have adequate backups of your data.

With practice and experience, you will become more comfortable and efficient with deleting documents in MongoDB. In the next chapter, we'll explore how to update documents in MongoDB, another essential task in maintaining a MongoDB database.

Now answer the exercise about the content:

Which of the following statements is true about deleting documents in MongoDB?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Deleting Documents in MongoDB: Introduction to Deleting Documents in MongoDB

Next page of the Free Ebook:

55Deleting Documents in MongoDB: Introduction to Deleting Documents in MongoDB

3 minutes

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