11.2. Deleting documents in MongoDB: Commands for deleting documents

Página 56

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:

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.

Next page of the Free Ebook:

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

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