Last Updated: 30 May, 2023
To drop a collection in MongoDB, you can use the drop() method.
Following are the steps we need to follow, as given below:
Select the database that contains the collection we want to drop. We can do this by using the use command and the database name.
For example, if our database is named "schoolDB", run the following command in the MongoDB shell:
use schoolDB
Once we're inside the correct database, we can use the drop() method to remove the collection as given in the example below.
db.students.drop()
Here, "students" is a collection of a "schoolDB" database.
After executing the drop() command, MongoDB will remove the specified collection from the current database. Please note that this operation is irreversible, and all the data stored in that collection will be permanently deleted.
Remember to exercise caution when using the drop() method, as it can lead to data loss. Make sure you have a backup of your data or have confirmed that you no longer need the collection before performing this action.
That's all, guys. I hope this MongoDB article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com