Deleting documents in MongoDB: Commands for deleting documents

Capítulo 56

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

In section 11.2 of our e-book course, we will cover a fundamental aspect of MongoDB: document deletion. MongoDB is a document-oriented NoSQL database that offers high performance, high availability and easy scalability. It works on the concept of collections and documents, unlike relational databases that work on tables and records.

Documents are the equivalent of records or rows in relational databases, but are more expressive. They have a rich and variable structure, and the fields can contain other documents, arrays and arrays of documents. Deleting documents is a common and important operation in any database management system. In MongoDB, the delete operation is done using the 'remove' or 'delete' command. There are several ways to delete documents in MongoDB, depending on the user's specific needs.

To begin with, the basic command to delete a single document in MongoDB is 'deleteOne'. This command deletes the first document that matches the given condition. For example, to delete a document where the 'name' field equals 'John', you would use the following command:

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

The 'deleteOne' command will return an object that contains information about how the operation was performed. If a document was successfully deleted, the 'deletedCount' field will be '1'. If no documents match the given condition, no documents will be deleted and the 'deletedCount' will be '0'.

If you want to delete multiple documents that match a certain condition, you can use the 'deleteMany' command. This command deletes all documents that match the given condition. For example, to delete all documents where the 'age' field is less than '18', you would use the following command:

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

db.collection.deleteMany({ age : { $lt : 18 } })

Just like the 'deleteOne' command, the 'deleteMany' command will return an object that contains information about how the operation was performed. The 'deletedCount' field will indicate the number of documents that were deleted.

Also, if you want to delete all documents from a collection, you can use the 'deleteMany' command without providing any conditions. For example, the following command will delete all documents from the collection:

db.collection.deleteMany({})

It is important to note that deleting documents in MongoDB is an irreversible operation. Once a document is deleted, it cannot be recovered. Therefore, it is crucial to be sure that you really want to delete a document before performing the delete operation.

Finally, it is important to remember that deleting documents can affect database performance. If you are deleting a large number of documents, the operation may take some time and consume significant system resources. Therefore, it is recommended to plan and optimize deletion operations to minimize the impact on system performance.

In summary, MongoDB offers powerful and flexible commands for deleting documents. However, it is important to use them carefully and fully understand their implications before carrying them out.

We hope this chapter has provided a clear and understandable overview of how to delete documents in MongoDB. In the next chapter, we will explore other important aspects of MongoDB, such as updating documents and performing complex queries.

Now answer the exercise about the content:

Which of the following commands is used to delete a single document that matches a specific condition in MongoDB?

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

You missed! Try again.

The correct command to delete a single document in MongoDB is deleteOne. This command targets the first document that matches the specified condition, removing it from the collection. The other options, like deleteAll or deleteSome, are not valid MongoDB commands for this purpose.

Next chapter

Deleting documents in MongoDB: Using the 'remove' command in MongoDB

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

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.