In the last article, you have read about MongoDB Create Collection Command. In today’s article, we will discuss MongoDB Drop Collection Command With Example. I hope you are already aware of collections are. If you do not know, here is the brief introduction – Collections are like a container for related documents. Like in SQL we have Tables, in NoSQL(such as MongoDB) we have COLLECTIONS.
MongoDB Drop Collection Command
For creating a collection in MongoDB, there are two ways to do – either by creating collection explicitly or creating a collection on the fly. However, there is only one MongoDB drop collection command. In MongoDB, db.collection.drop() method is used to drop a collection from a database. The db.collection.drop() method does not take any argument and produce an error when it is called with an argument. It completely removes a collection from the database and does not leave any indexes associated with the dropped collections. This method also removes all the indexes associated with the dropped collection.
MongoDB Drop Collection Command Example
Below is an example to drop collection in MongoDB. Before dropping any collection, let’s see what are the collections existing in my database.
To do so, start your Mongo server and Mongo Shell. Write below commands:
MS DOS12345678910111213141516171819202122> show databasesadmin 0.000GBblog 0.011GBenron 0.211GBlocal 0.000GBm101 0.000GBmohit 0.017GBschool 0.000GBstudents 0.000GBtest 0.002GBvideo 0.001GBweek5 0.002GB
MS DOS12> use mohitswitched to db mohit
MS DOS12345678910> show collectionsalbumsimagesmohitpoststutorialsJar
Note: Here. I have first listed all the databases available in my system. Then I have chosen database “mohit”. Here you can see, we have a collection called “tutorialsJar”. Let’s delete this collection now.
Below is the MongoDB drop collection command –
> db. tutorialsJar.drop()
Note: The drop command returns true if it successfully drops a collection. It returns false when there is no existing collection to drop.
Now, let’s check the collections again in the database to ensure if the collection is deleted:
MS DOS12345678> show collectionsalbumsimagesmohitposts
You can see there is no tutorialsJar collection and is deleted now.
The key point to be noted here is that This drop () command obtains a write lock on the affected database and will block other operations until it has completed.
That’s all in this article. In upcoming articles, we will learn about CRUD operations on collections in MongoDB.
I hope you all liked this article on MongoDB Drop Collection Command. Don’t Forget To Share It with your friends and subscribe to our Email newsletter for more such updates. If you have any questions, please feel free to ask in the comments section below.
Latest posts by Mohit Arora (see all)
- MongoDB Projection Tutorial : Return Specific Fields From Query - March 9, 2019
- MongoDB Index Tutorial – Create Index & MongoDB Index Types - July 6, 2018
- MongoDB Find Command To Query Documents - December 4, 2017