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

What is MongoDB Drop Col­lec­tion?

The structure of the NoSQL database man­age­ment system MongoDB is very different to re­la­tion­al database solutions like MySQL. In par­tic­u­lar, NoSQL systems are set up in a way that allows for more scalab­il­ity and flex­ib­il­ity. In such a system, data is stored in documents, which are then organized into col­lec­tions. While MongoDB auto­mat­ic­ally creates col­lec­tions, you also have the option as the user to set them up manually in MongoDB with Create Col­lec­tion. By setting col­lec­tions up yourself, you can define certain para­met­ers, like the maximum size of a document. If, however, you want to get rid of a col­lec­tion, you can use drop col­lec­tion 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 Col­lec­tion Syntax

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

db.COLLECTION_NAME.drop()

How to use MongoDB Drop Col­lec­tion

In order to explain how to correctly use MongoDB Drop Col­lec­tion, it’s helpful to have an example. Let’s say that we have set up a client list that is organised by country, with each col­lec­tion in the data bank cor­res­pond­ing to a different country. Now, we need to delete the col­lec­tion with clients from the UK.

Get an overview of all col­lec­tions

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

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

The col­lec­tion that we need (`unitedk­ing­dom´) has been located in the database.

Use the MongoDB Drop Col­lec­tion command

Next, carry out the command to remove the col­lec­tion. Using our example, the command looks like this:

>db.unitedkingdom.drop()

Confirm removal

If you have suc­cess­fully carried out the command, you will see the following:

>db.unitedkingdom.drop()
true
>

If, however, you were not able to suc­cess­fully remove the col­lec­tion, the value `false´ will appear. This could be the result of a spelling error or perhaps, the col­lec­tion doesn’t actually exist.

Tip: Managed MongoDB from IONOS

Managed MongoDB from IONOS enables you to con­cen­trate on the es­sen­tials. From in­stall­a­tion to operation and main­ten­ance work, IONOS makes sure you always get the best per­form­ance from your data banks.

Check col­lec­tions in the database again

To be certain that the MongoDB Drop Col­lec­tion command was suc­cess­fully carried out, it’s important to go back and check all the col­lec­tions in your selected database. You can do this by repeating the show col­lec­tions command.

>show collections
unitedkingdom
france
italy
>
What’s the dif­fer­ence between MongoDB Drop Col­lec­tion and MongoDB Remove?

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

Go to Main Menu