How to remove collections from MongoDB with Drop Collection

You can remove a collection from a specific database by using the drop collection command. Unlike the remove command, this removes all links and indices in the collection as well.

What is MongoDB Drop Collection?

The structure of the NoSQL database management system MongoDB is very different to relational database solutions like MySQL. In particular, NoSQL systems are set up in a way that allows for more scalability and flexibility. In such a system, data is stored in documents, which are then organized into collections. While MongoDB automatically creates collections, you also have the option as the user to set them up manually in MongoDB with Create Collection. By setting collections up yourself, you can define certain parameters, like the maximum size of a document. If, however, you want to get rid of a collection, you can use drop collection to remove it from the database.

Tip

Would you like to learn more commands? Then take a look at our overview at MongoDB commands.

The MongoDB Drop Collection Syntax

The MongoDB Drop Collection command is simple and always follows the same syntax. Here’s how to write it:

db.COLLECTION_NAME.drop()

How to use MongoDB Drop Collection

In order to explain how to correctly use MongoDB Drop Collection, it’s helpful to have an example. Let’s say that we have set up a client list that is organized by country, with each collection in the data bank corresponding to a different country. Now, we need to delete the collection with clients from the United States.

Get an overview of all collections

Before you use drop collection, you’ll first want to give yourself an overview of all the collections you have. You can do this by using the show collections command. Here is an example of the syntax for this command:

>use clientlist
switched to db clientlist
>show collections
unitedstates
unitedkingdom
france
italy
>

The collection that we need (“unitedstates”) has been located in the database.

Use the MongoDB Drop Collection command

Next, carry out the command to remove the collection. Using our example, the command looks like this:

>db.unitedstates.drop()

Confirm removal

If you have successfully carried out the command, you will see the following:

>db.unitedstates.drop()
true
>

If, however, you were not able to successfully remove the collection, the value “false” will appear. This could be the result of a spelling error or perhaps, the collection doesn’t actually exist.

Check collections in the database again

To be certain that the MongoDB Drop Collection command was successfully carried out, it’s important to go back and check all the collections in your selected database. You can do this by repeating the show collections command.

>show collections
unitedkingdom
france
italy
>
What’s the difference between MongoDB Drop Collection and MongoDB Remove?

In addition to the MongoDB Drop Collection command, there is also the remove command. Despite having similar syntax, this method only removes a document within the collection, leaving the indices you created using MongoDB Create Index intact. If you want to remove the collection as well as all of its links, make sure to use the MongoDB Drop Collection command.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top