Node.js MongoDB Drop

Node.js MongoDB Drop

Dropping means permanently deleting a database or collection.

MongoDB provides two drop options:

  1. Drop a Collection

  2. 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

TaskMethod
Drop Collectioncollection.drop()
Drop Databasedb.dropDatabase()

You may also like...