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?
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.
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.
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
Now for dropping the teacher collection from the database gfg, we will use the drop collection method:
The method will drop the teacher collection and its documents. The result will be as follows:
[> show collections
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.
- Drop Database In MongoDB
- Create a Database in MongoDB
- Create Collection in MongoDB
- Data Types In MongoDB
- Indexing in MongoDB
Are you considering a profession in the field of Data Science? Then get certified with the Data Science Bootcamp today!
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 Program. 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.