The MongoDB drop collection
operation is used to delete an existing collection from a database. This command permanently removes the collection and all its documents, making it a powerful and irreversible action.
db.collection.drop()
The db.collection.drop()
method drops the specified collection.
Let's look at some examples of how to drop a collection in MongoDB:
db.programGuru.drop()
This command drops the programGuru
collection from the current database.
show collections
This command lists all collections in the current database. You should no longer see programGuru
in the list.
Let's go through a complete example that includes switching to a database, dropping a collection, and verifying its deletion.
This step involves switching to a database named myDatabase
.
use myDatabase
In this example, we switch to the myDatabase
database.
This step involves dropping a collection named programGuru
in the myDatabase
database.
db.programGuru.drop()
Here, we drop the programGuru
collection.
This step involves listing the collections in the myDatabase
database to verify that programGuru
has been deleted.
show collections
This command lists all collections in the current database. You should not see programGuru
in the list.
The MongoDB drop collection
operation is a critical command for deleting collections. It is essential to use this command carefully, as it permanently removes the collection and all its documents.