MongoDB Drop Collection Command With Example

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.

Syntax:

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:



MongoDB Drop Collection Command With Example

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 –

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:


  1. MongoDB Drop Collection Command With Example1

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. 

Mohit Arora
Follow me

Mohit Arora

Hi, I am Mohit Arora , a passionate blogger and addicted reader. Currently working as a Senior Systems Engineer in INFOSYS Ltd.
You can check my other blog - CatchUpdates.com

You can connect with me on Instagram Or Quora And Linkedin as well.
Mohit Arora
Follow me