MongoDB Drop Collection

MongoDB – Drop Collection

If you want to drop a collection, then first you have to choose the database, where the specific collection is present. Then collection will be deleted, by the use of below command:

Syntax:

db.collection.drop()

Once a collection is dropped, all the indexes associated with it and the documents are also dropped with it. But, if you want to remove only the documents and not the collection and indexes associated, then use remove() function.

Note: The method db.collection.drop() does not take any argument, and if called with an argument, then it produces an error.

Example: MongoDB: db.collection.drop() method

1. Connect to the database where you can find your collection.

>use productDB
 switched to productDB 

2. Check for already existing collections in the database.

>Show collections
 product
 product_description
 product_inventory
 product_sales
 system.indexes 

3. Now, you can drop any specific collection by specifying its name

>db.product_sales.drop()
 true
 >  

4. You can check for collection in the database by below command:

show collections

You will not find the dropped collection. The drop() method returns true only if it successfully drops a selected collection, else it returns false.

MongoDB db.collection.drop() method