MongoDB Drop Collection

In this chapter, we will show you how to delete collections using MongoDB.

In MongoDB, use the drop() method to delete a collection.

The drop() method permanently deletes the specified collection and all its documents from the database. This is an irreversible operation, so it should be used with caution.

Syntax format:

db.collection.drop()

Parameter description:

  • None

Return Value

If the selected collection is successfully deleted, the drop() method returns true; otherwise, it returns false.

Example

In the database mydb, we can first use theshow collectionscommand to view existing collections:

>use mydb
switched to db mydb
>show collections
mycol
mycol2
system.indexes
example
>

Then delete the collection mycol2:

>db.mycol2.drop()
true
>

Use show collections to view the collections in the database mydb again:

>show collections
mycol
system.indexes
example
>

From the results, you can see that the mycol2 collection has been deleted.

Other extensions