MongoDB

MongoDB Delete Document

The documents in MongoDB are defined as the rows and records within a collection. These documents hold the data that you intend to store inside a MongoDB database. A document in MongoDB can have multiple fields or columns or attributes associated with it. A MongoDB collection can have single or multiple documents inside it. It is extremely easy to insert document[s] inside a MongoDB collection. However, when it comes to deleting these documents, many people often get confused because there are multiple functions that can be used to serve this purpose. But each of these has a distinct functionality. Therefore, in this article, we have decided to share with you the two most common and the easiest methods of deleting document[s] from a collection in MongoDB in Ubuntu 22.04.

Two Methods of Deleting Document in MongoDB in Ubuntu 22.04:

The two most commonly used methods for deleting document in MongoDB in Ubuntu 22.04 are discussed below:

Method # 1: Deleting a Single Document in MongoDB in Ubuntu 22.04:

In this method, we will be sharing with you the steps using which you will be able to delete a single document from a collection of a MongoDB database. These steps are as follows:

Step # 1: Switching to the MongoDB Database whose Document you want to delete:

After accessing the MongoDB shell, the first thing that you need to do is to switch to that MongoDB database that contains the collection whose document you wish to delete. In this example, we wanted to switch to the “SampleDB” database because of which we have executed the query shown below:

> use SampleDB

Upon switching to this database successfully, we received the following message as the output:

Step # 2: Viewing all the Documents of the Target Collection:

Now, for figuring out the exact document that we want to remove from the target collection of the database we have just switched to. We will first view all the documents of the target collection which in this case is “SampleCollection”. For doing so, we have used the query shown below:

> db.SampleCollection.find( {} )

This query will simply display all the records or documents of the specified collection as shown in the following image:

Step # 3: Deleting the Desired Document from the Target Collection:

Out of all the documents shown above, we wanted to delete the second document for the sake of demonstration. For deleting this document from our target collection, we have used the query shown below:

> db.SampleCollection.remove( { name:“Clark” } )

Here, we have used the name field of the document as our deletion criteria. You can even use the age field over here or any other field if it exists within your particular collection for deleting a single document.

The following message will be displayed on the MongoDB shell upon successful deletion of the specified document from the target collection.

Step # 4: Verifying if the Specified Document has been deleted or not:

Although, we have already received a success message upon executing the “remove” query for a single document. However, we can double check it by using the “db.SampleCollection.find( {} )” query again as shown in the image below. Here, you can see that the document that we attempted to delete no longer exists within our collection. It implies that we have successfully managed to delete the specified document from our target collection in MongoDB.

Method # 2: Deleting all the Documents in MongoDB in Ubuntu 22.04:

In this method, we will teach you how you can delete all the documents from your target collection in MongoDB in Ubuntu 22.04. However, before proceeding with this method, you will have to switch to your desired database first just as we did in the first step of our first method. After switching to that database, you will easily be able to delete all the documents from your target collection by carrying out the following steps:

Step # 1: Deleting all the Documents from the Target Collection:

For deleting all the documents from the collection named “SampleCollection”, we will use the query shown below:

> db.SampleCollection.remove( {} )

This time, since we have not specified any deletion criteria within the “remove” function, this query will attempt to delete all the documents of the specified collection in MongoDB.

When all the documents from your specified collection will be deleted, you will receive the following success message specifying the total number of documents that have been deleted as a result of the execution of this query.

Step # 2: Verifying if all the Documents have been deleted or not:

To verify if all the documents of our target collection have been deleted successfully, we will use the “db.SampleCollection.find( {} )” query. This time, this query will not display any output because no documents exist within our target collection anymore. This can be seen from the image shown below:

Conclusion:

This guide was designed to explain to you the deletion of document[s] in MongoDB in Ubuntu 22.04. We first introduced you to the term “document” which is used most frequently while working with MongoDB. After that, we shared with you two methods using which you can conveniently delete document[s] from a collection in MongoDB in Ubuntu 22.04. If you wish to delete a single document from a MongoDB collection, then you can make use of the first method for which you only need to specify the deletion criteria with the “remove” function. However, if you intend to delete all the documents of the MongoDB collection at once, then you can use the second method discussed in this article. For using the second method, you simply need to use the “remove” function without specifying any deletion criteria. By using either of these methods depending upon your requirements, you will instantly be able to delete document[s] in MongoDB while using a Ubuntu 22.04 system.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.