MongoDB Drop Collection Examples
Let’s look at some of the examples of db.collection.drop() method in MongoDB. These examples demonstrate how to delete a collection in MongoDB.
In the following example, we are working with:
Database: gfg
Collection: student
Document: Three documents contains name and the age of the students
Example 1
We will drop the student collection in the gfg database.
db.student.drop()
It drops the student collection and all the indexes associated with the collection:
Output
Example 2
In the following example, we are working with:
Database: gfg
Collections: student_gfg, teacher, semester
Here, we want to drop teacher collection from the gfg database. So, we use drop method:
db.teacher.drop()
This method drops teacher collection along with its documents.
Output
MongoDB – Drop Collection
The MongoDB db.collection.drop() method removes a collection from the database. It also removes all indexes associated with the dropped collection.
Important Points
- This method creates an invalidate Event for any change streams opened for dropped collection.
- This method obtains an exclusive lock on the given collection for the duration of the operation. Till then all the subsequent operations must wait for the drop() method to release the lock.
Contact Us