Deleting documents in MongoDB

Capítulo 54

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

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.

Option 3 is correct as the deleteMany() function can indeed delete all documents in a collection without specifying any filter by passing an empty filter object {}.

Next chapter

Deleting Documents in MongoDB: Introduction to Deleting Documents in MongoDB

Arrow Right Icon
Free Ebook cover Creation and maintenance of MongoDB database from basic to advanced
61%

Creation and maintenance of MongoDB database from basic to advanced

5

(1)

88 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.