Node.js MongoDB Drop
✅ Node.js MongoDB Drop
Dropping means permanently deleting a database or collection.
MongoDB provides two drop options:
Drop a Collection
Drop a Database
⭐ 1. Drop a Collection
This deletes the entire collection and all documents inside it.
Example: Drop the customers collection
If the collection does not exist, MongoDB will throw an error.
To avoid errors, check if the collection exists:
⭐ 2. Drop a Database
⚠ This removes ALL collections in the database.
Example: Drop the database mydb
⭐ 3. Full Example Program
⭐ Summary Table
| Task | Method |
|---|---|
| Drop Collection | collection.drop() |
| Drop Database | db.dropDatabase() |
