With 15M+ downloads, MongoDB is amongst the most popular cross-platform, open-source, free NoSQL databases, empowering users to query, manipulate and obtain valuable insights from their data. Its flexible document data model allows 3–5x faster iteration. Drop collection in MongoDB is a useful command for removing collections from a database. Learn all about this command, its use, parameters, and examples here. 

What is Drop Collection in MongoDB?

Drop collection in MongoDB is a method used to drop a collection from a database. It gets rid of all associated indexes of the removed collection as well. 

Become a Data Scientist With Real-World Experience

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist With Real-World Experience

The db.collection.drop() command doesn’t take any argument. It results in an error if it is called with an argument. The method takes input in the form of an optional document. The output returns false if there is no existing collection to drop. It returns true when the collection is successfully dropped. 

Drop collection creates an invalidate Event for a change stream/ streams opened for dropped collection. It obtains a lock on the given collection during the operation. All subsequent operations wait until the method releases the lock.

The db.collection.drop() method returns an error when you try to drop a collection in the admin database or the config database. For dropping these collections, connect to the config server and run the command there.

Syntax: 

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

Use of Drop Collection in MongoDB

To remove a collection completely, use the db.collection.drop() method and drop the entire collection, including the indexes and then reuse the collection. A user can recreate the collection and rebuild the indexes following the removal. The approach helps avoid flushing the cache.

Parameters of Drop Collection in MongoDB

No parameters are taken in MongoDB, but it can take one optional parameter. 

Document, called optional parameter, expresses the write concern of the drop collection in MongoDB operation. The users can use it if they want to omit the use of the default write concern.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

MongoDB Drop Collection Example 

The following example is for a database gfg. The collections are student_gfg, teacher, and semester.

Begin by checking the available collections in your database. Here the database is gfg.  

[> use gfg

 switched to db gfg

[> show collections

 student_gfg

 teacher

 semester

 >

Now for dropping the teacher collection from the database gfg, we will use the drop collection method:

db.teacher.drop()

The method will drop the teacher collection and its documents. The result will be as follows:

[> db.teacher.drop()

 true

[> show collections

 student_gfg

 semester

 >

FAQs

1. How to drop multiple collections in MongoDB?

The db.collection.deleteMany() method allows deletion of the existing documents from the collection. It removes multiple documents from the collection. 

2. How to drop a collection in MongoDB Atlas?

The drop command removes the specified view or collection from the MongoDB Atlas Data Lake storage configuration. The wildcard "*" can be used to drop all collections that the wildcard collection function generates, including the wildcard collection rule. 

3. How to restore a dropped database in MongoDB?

One of the most accurate and reliable ways to recover deleted databases is using a Supported Backup Method. Opt for a continuous backup like that offered in the MongoDB Ops Manager. The Ops Manager grants the Queryable backup feature for selective recovery of data.

Conclusion

The demand for qualified MongoDB experts is on the rise. Learn more about MongoDB and advance your skill set. Begin your journey towards becoming a better data professional with Data Science Certification. Gain valuable insights from IBM experts and Purdue faculty to fast-track your career and lift confidence by adding a star to your resume before your next Data Science interview. 

Our Big Data Courses Duration And Fees

Big Data Courses typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Post Graduate Program in Data Engineering

Cohort Starts: 5 Apr, 2024

8 Months$ 3,850