MongoDB Drop Collection

Learn via video courses
Topics Covered

Overview

MongoDB drop collection is used for removing views or collections from the database. And all the indexes associated with the collection to be dropped are also dropped with the MongoDB drop collection. One optional parameter is taken by the MongoDB drop collection, except that no other parameters are taken by the MongoDB drop collection.

What is MongoDB Drop Collection?

MongoDB drop collection is used for removing views or collections from the database. All the indexes that are related to the dropped collection are also dropped by this method.

Syntax

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Values

  • When the collection is dropped successfully then it returns true.
  • When a collection required to be dropped does not exist then false is returned by it.

Parameters

MongoDB drop collection takes only one optional parameter, except that no other parameters are taken by the MongoDB drop collection.

The optional parameter taken by it is called the document by which the write concern of the drop collection is represented. Users can use this parameter if the requirement is to omit the default write concern.

Uses

dp.collection.drop() method is used for the complete removal of a collection inclusive of the indexes of that collection. And the collection can be again created by the user after the removal and they can build all the indexes again.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

MongoDB Drop Collection Behavior

  • There is a creation of an invalidate by the db.collection.drop() method and drop command for change streams opened on dropped collection.
  • After MongoDB 4.4, It is possible for the drop command and the db.collection.drop() method to abort any in-progress index builds available on the target collection even before the collection dropping. In the versions previous to MongoDB 4.4, it is not possible to drop any in-progress index builds available on the target collection as it results in the creation of an error and leads to the failure of dropping the collection.
  • If the index on the primary is aborted for the shard replica sets or replica sets, then it doesn't mean that the secondary index will also abort simultaneously.

How to Reuse Dropped Collection Name as Sharded Clusters

In a sharded cluster, if the collection is created by you with the name same as the collection dropped previously then in the versions earlier to MongoDB 5.0, the operations may be forwarded by the mongos to the incorrect shard. To avoid this problem, perform operations according to the instructions given below according to the versions of MongoDB.

  • No special action is needed for the sharded cluster running MongoDB version 5.0 or later versions.
  • And for the sharded cluster running MongoDB version 4.4 and its prior version, if the drop() method is used by you then the same collection is created by you having a name similar to the dropped collection. Then you must do one of the following:
    • For removing existing documents and for reusing them, use the db.collection.remove().
    • Use flushRouterConfig for flushing the cached routing table on each mongos.

Resource Locking in MongoDB Drop Collection

For the operation duration, an exclusive lock is obtained by the db.collection.drop() on the collection specified in the command. All the other operations require waiting until the lock is released by the db.collection.drop().

In the versions previous to MongoDB 4.2, an exclusive lock is obtained by the db.collection.drop() on the parent database. Due to this all the operations on the Database and all the operations on all the collections of the database are blocked during the drop operation.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Examples

Drop a Collection Using Default Write Concern

The collection has the named employee in the current database dropped by the command given below:

Drop a Collection Using w: 1 Write Concern

The collection having the name employee in the current database is dropped by the command given below with the write concern 1:

FAQs

Question:- Explain the method for dropping multiple collections in MongoDB?

Answer:- For deleting multiple documents from the collection, db.collection.deleteMany() command is used in MongoDB.

Question:- How can a collection be dropped in MongoDB Atlas?

Answer:- Drop command is used for removing the particular collection or view from the storage configuration of MongoDB Atlas Data Lake. All the collections generated by the wildcard collection function can be dropped by the * wildcard.

Question:- Explain the way to restore the database that is dropped?

Answer:- Supported Backup Method is one of the most reliable and accurate methods of restoring the deleted database. Continuous backup should opt such as provided in the MongoDB Ops Manager. A queryable backup feature is granted by the Ops Manager for selective data recovery.

Conclusion

  • MongoDB drop collection is used for removing views or collections from the database.
  • MongoDB drop collection takes only one optional parameter, except that no other parameters are taken by the MongoDB drop collection.
  • dp.collection.drop() method is used for the complete removal of a collection inclusive of the indexes of that collection.
  • For the operation duration, an exclusive lock is obtained by the db.collection.drop() on the collection specified in the command.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more