The db.CollectionName.drop() Method in MongoDB in Ubuntu 22.04
The method that is used to drop a MongoDB collection in Ubuntu 22.04 is “db.CollectionName.drop()”. This method simply removes all the traces of the specified collection from your current database, including all the data residing in it and all the associated indexes. All you need to ensure for this method to work as intended is that;
1) You have already switched to the database where the said collection resides and,
2) You know the correct name of the collection you wish to remove.
If you take care of these two things, then you will easily be able to remove any desired collection from a MongoDB database without any issues.
Drop Vs. Remove in MongoDB
There is also a remove method in MongoDB along with the drop method. People are often confused about which method they should use to delete a MongoDB collection in Ubuntu 22.04. As far as the drop method is concerned, we have already mentioned that it completely removes all the traces of the collection along with its associated indexes. However, on the other hand, the remove method removes a collection while deleting all the documents residing inside it but keeps the associated indexes intact. Therefore, if your goal is to get rid of a MongoDB collection completely, then it is recommended to use the drop method of MongoDB.
How to Drop a Collection in MongoDB in Ubuntu 22.04?
For dropping a collection or a table in MongoDB in Ubuntu 22.04, you have to perform the following five basic steps:
Step # 1: Entering the MongoDB Shell
We all understand that without entering the MongoDB shell (unless we have the MongoDB Compass tool installed), we can’t execute our queries for interacting with the databases. Therefore, the first step is to enter the MongoDB shell with the command given below:
The shell of MongoDB where you can execute your queries is shown in the following image:
Step # 2: Switching to the MongoDB Database Where the Collection to be Dropped Resides
Before doing anything with the collections, we must navigate to the database where the target collection resides. Without doing this, we cannot interact with our desired collection. Therefore, we will switch to the target database with the command displayed beneath:
“CollectionsDB” is the name of the MongoDB database to which we want to switch. The following message will imply that you have successfully switched to your target database.
Step # 3: Observing All the Collections of the Present Database
Since we understand that a MongoDB database can have multiple collections or tables, for extracting the correct name of the collection to be dropped, we will attempt to view all the collections of the database that we have just switched to. We will do this with the command shown beneath:
This command will list down all the collections of the present database. The output of this command, in our case, is shown in the image beneath. You can see that we only have one collection in our current database.
Step # 4: Dropping the Desired Collection
Now, we will attempt to drop the only collection that we have in our current database by running the query given below:
Here, you need to use the correct name of the collection you wish to eliminate; otherwise, you might drop some other important collection you never really intended to drop.
When your specified collection, along with all the data residing in it will, is dropped successfully, you will get a “true” message on the MongoDB shell, as shown in the following image:
Step # 5: Confirming if the Specified Collection Has Been Dropped or Not
We will try to confirm if the specified collection has been dropped successfully or not for added assurance. For that, we will try to display all the collections of our current database again with the query stated below:
This time, this query will not return any output since we have successfully dropped the only collection in our current database. This can be viewed in the subsequent image. However, if you will have more than one collection in your current database, then after dropping any one of them, when you will run the above-mentioned query, you will see that the dropped collection no longer exists in that list. From there, you will be able to verify that you have successfully managed to drop the specified collection from your current database.
Conclusion
We wanted to focus on the drop collection method of MongoDB in Ubuntu 22.04 in this article. We first talked a bit about this method, followed by its distinction from the remove method of MongoDB. After that, we shared a detailed example with you that demonstrated the usage of the drop collection method of MongoDB in Ubuntu 22.04. By following the very same lines, you can delete or drop as many collections from a MongoDB database as you want with absolute ease and convenience.