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:

db.COLLECTION_NAME.drop()

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:

  1. > show databases
    admin     0.000GB
    
    blog      0.011GB
    
    enron     0.211GB
    
    local     0.000GB
    
    m101      0.000GB
    
    mohit     0.017GB
    
    school    0.000GB
    
    students  0.000GB
    
    test      0.002GB
    
    video     0.001GB
    
    week5     0.002GB
    > use mohit 
    switched to db mohit
    > show collections
    albums
    
    images
    
    mohit
    
    posts
    
    tutorialsJar
    
    

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 –

 > db. tutorialsJar.drop()  

True

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. > show collections
    albums
    
    images
    
    mohit
    
    posts

    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