MongoDB Drop Collection
MongoDB Drop Collection
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.
Syntax
db.collection.drop()
The db.collection.drop()
method drops the specified collection.
Example MongoDB Drop Collection
Let's look at some examples of how to drop a collection in MongoDB:
1. Drop a Collection
db.programGuru.drop()
This command drops the programGuru
collection from the current database.
2. Verify Collection Deletion
show collections
This command lists all collections in the current database. You should no longer see programGuru
in the list.
Full Example
Let's go through a complete example that includes switching to a database, dropping a collection, and verifying its deletion.
Step 1: Switch to a Database
This step involves switching to a database named myDatabase
.
use myDatabase
In this example, we switch to the myDatabase
database.
Step 2: Drop a Collection
This step involves dropping a collection named programGuru
in the myDatabase
database.
db.programGuru.drop()
Here, we drop the programGuru
collection.
Step 3: Verify the Collection Deletion
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.
Conclusion
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.