📅  最后修改于: 2023-12-03 15:17:41.611000             🧑  作者: Mango
In MongoDB, a collection is a group of MongoDB documents. It is equivalent to a table in relational databases. Sometimes, we may want to delete a collection from the database. In this tutorial, we will learn how to use the MongoDB command to drop a collection.
Before we begin, you should have:
The basic syntax of the drop()
method in MongoDB is:
db.collection.drop()
Suppose we have a database named sample_database
with a collection named sample_collection
. We can drop the sample_collection
using the drop()
method as follows:
use sample_database
db.sample_collection.drop()
If the sample_collection
exists, it will be deleted from the database. And if it doesn't exist, MongoDB will return a message saying that the collection does not exist.
In this tutorial, we have learned how to use the drop()
method in MongoDB to delete a collection. It is important to note that once a collection is dropped, the data in that collection is also deleted permanently. Therefore, make sure to take backups before deleting any collections.