Open In App
Related Articles

MongoDB – Drop Collection

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Last Updated : 28 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials