Open In App

MongoDB – Drop Collection

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax

db.Collection_name.drop({writeConcern: <document>})

Parameters:

It does not take any parameters.

Optional Parameters:

This parameter is a document expressing the write concern of the db.collection.drop() operation. It is used when we want to omit to use of the default write concern.

Returns:

This method returns true when successfully drops a collection, otherwise returns false.

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

demo mongodb collection and database

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

drop collection method example 1

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

drop collection method example 2

Conclusion

This guide covers the use of db.collection.drop() method to remove collection from the database in MongoDB. We also covered some important points on the collection drop method to get a proper idea of its functioning.

At last, we saw two examples of the drop collection method in MongoDB. These examples are also explained with easy-to-understand explanations.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads