Skip to content
Related Articles

Related Articles

MongoDB – Drop Collection
  • Last Updated : 28 Jan, 2021
GeeksforGeeks - Summer Carnival Banner

The drop() method removes collections from the database. It also removes all indexes associated with the dropped collection. The drop method accepts an optional document. It returns true if the collection dropped successfully. It takes one parameter which is optional.

  • 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 parameter.



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.

Example 1 :

In the following example, we are working with:

Database: gfg

Collection: student



Document: Three documents contains name and the age of the students

We have student collection in the gfg database, and we want to drop it when we give commend:

db.student.drop()

It drops the student collection and all the indexes associated with the collection:

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.


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :