MongoDB drop collection – Complete Guide

In this MongoDB tutorial, We are going to learn “How can we drop the collection”. We will also cover this with different operations and examples. These are the following topics that we are going to cover in this tutorial:

  • What is drop collection in MongoDB?
  • MongoDB drop collection
  • MongoDB drop collection command
  • Drop collection using MongoDB compass
  • MongoDB drop collection if exists
  • MongoDB drop collection if empty
  • MongoDB drop collection by using shell
  • MongoDB drop collection which has space in between the name
  • MongoDB drop collection by name
  • MongoDB drop collection not working
  • MongoDB drop collection using python

What is drop collection in MongoDB?

When end-user wants to remove documents inside the collection from the database then we drop the collection. For dropping the collection we use the drop() method. It also doesn’t leave any indexes with the dropped collection.

Syntax:

db.collection_name.drop()

the db.collection_name.drop() command is used to drop the collection and this method also doesn’t take any argument.

Note that, if you give an argument then this will produce an error so always remember you don’t need to give any argument.

Also Read: How does MongoDB create a collection

MongoDB drop collection

In this topic, you will learn in detail how to drop a collection in MongoDB and I will give you an example that helps you to easily understand the full process of dropping the collection.

Example:

In this example, you will learn how to drop the collection so you need to follow the below steps:

  • show databases this command will show you all the database in your MongoDB database.
> show databases
admin       0.000GB
config      0.000GB
local       0.000GB
Sales       0.000GB

Here, you can see the Sales database that we created earlier.

  • show collections this will show you all the collection inside the database.
> use Sales
switched to db Sales

> show collections
products

Here, you can see we have only one collection(products) in the Sales database.

  • Now for dropping the collection you need to use drop() method.
> db.products.drop()
true

Here, output true means we successfully drop the collection.

  • Now check the collection again to see that collection is drop or not.
> show collections
>

This shows you nothing means the collection(products) drop successfully.

Note, The output displays true which means the collection successfully drops and if it returns false then there is no existing collection to drop.

Read: MongoDB backup and restore

MongoDB drop collection command

In MongoDB, the drop command will help you to remove the entire collection from the database.

Syntax:

{ drop: <collection_name>, writeConcern: <document>, comment: <any> }
FieldDescription
dropName of the collection to drop
writeConcernOptional, A document expressing the write concern of the drop command.
commentOptional, user-provided comment to attach the command.
drop command fields

Example:

In this example, we drop the collection

> show databases
admin       0.000GB
config      0.000GB
local       0.000GB
Product     0.000GB

> use Product     
switched to db Product     

> show collections
EmpData

In the next step, we drop the EmpData collection from the Product database.

> db.EmpData.drop()
true

Here, we successfully drop the EmpData collection.

Remember that, when we drop the collection it also doesn’t leave any indexes with the dropped collection.

Read: Create tables in MongoDB

Drop collection using MongoDB compass

In this topic, You will learn how to drop the collection in MongoDB compass powerful GUI tool. Compass is free to use and source available and can be run on macOS, Windows, and Linux.

Example:

In this example, we learn how to drop a collection, so we created earlier a database name as an example and a collection called products.

Now you have to follow the below steps to drop the collection:

  • Open MongoDB compass
  • Select the database and collection that you want to drop
  • Now to drop this collection just click on three dots on right side of collection and this will show you a drop down list and here you need to click on Drop Collection.
Drop collection using MongoDB compass
Drop collection using MongoDB compass
  • This will a new window and you have to write the collection the collection name.
  • Click on DROP COLLECTION button and this will drop the collection from your database.
Drop collection using compass
Drop collection using compass

Here, you might have a question “why you have to write the collection name?Solution: In a database, there might be multiple collections so you have to specify the particular collection name which you want to drop that’s why you have to specify the collection name.

This way you can easily drop the collection in MongoDB compass.

Read: Pros and cons of MongoDB

MongoDB drop collection if exists

In this topic, you will learn to How to drop the collection if exists.

Note that, When a collection exists successfully dropped from the database then it will display true and if the collection does not exist then it will display false as output.

Example:

In this example, We drop the student collection from the College database.

Here, The show databases will show you all the databases you have created.

> show databases
admin       0.000GB
config      0.000GB
local       0.000GB
College     0.000GB

You can drop the collection by using the drop() method.

> db.student.drop()
false

This will show you output as false means, there is no collection(student) in the College database. It will only drop the collection when the collection exists in the database.

Read: MongoDB sort by date

MongoDB drop collection if empty

In this topic, you will learn how to drop collection if empty. First, you need to check the collection is empty or not, so check all the documents inside the collection. You can understand with an example this will help you to drop a collection if empty.

Example:

In the MongoDB, check the documents inside the collection

> db.profile.find().pretty()
{
        "_id" : ObjectId("61285f2224d4f081b3630154"),
        "item" : "card",
        "qty" : 15,
        "city" : "Canada"
}
{
        "_id" : ObjectId("61285f2224d4f081b3630155"),
        "item" : "envelope",
        "qty" : 20,
        "city": "United States of America"
}
{
        "_id" : ObjectId("61285f2224d4f081b3630156"),
        "item" : "stamps",
        "qty" : 30,
        "city": "United Kingdom"
}

Here the profile is the collection name and you can see there is data inside the collection. The collection is not empty so first we remove all the data inside the collection after that we can easily drop the collection.

> db.profile.remove({})
WriteResult({ "nRemoved" : 3 })

The remove() method removes all the data from the collection. Here, we give “{ }” brackets inside the remove method means to remove all the documents. You can also define a particular field if you want to remove it.

Now, you can drop the collection using the drop() method.

> db.profile.drop()
true

This command will drop the collection from the database. Here, true means collection successfully drops from the database.

Important: Here you might have a question

Question: Why you can not directly drop the collection?

Solution: Sometimes, we don’t want to drop the collection, we only want to remove data inside the collection so in that situation we use the remove() method.

The remove() method will remove all the documents inside the collection and you can also remove a particular field from the documents. The drop() method is used when we want to drop the collection with documents.

Read: MongoDB sort by field

MongoDB drop collection by using shell

To drop the collection using a shell, you need to follow the steps

Example:

  • Open command prompt and start the MongoDB server
Start MongoDB Server
Start MongoDB Server
  • Check all the databases and collections in MongoDB database
Drop collection using shell
Check databases and collections
  • Drop the Student collection by using drop() method.
Drop a collection in MongoDB using shell
Drop a collection in MongoDB using shell

Here, we drop Student collection using the drop() method, and true means the collection successfully drops from the database. If it displays false as output means there is not any(not exist) collection such as Student name.

Read: Import JSON and insert JSON into MongoDB

MongoDB drop collection which has space in between the name

A few days back, I was trying to drop the collection but getting the error because there was a space in between the name of the collection. This was the error I was getting

drop collection with space in the name
Error while dropping the collection which has
space in the name

After a lot of research, I found the solution to this problem and this will resolve the error and you can easily drop the collection.

You have to use the getCollection() method. The getCollection() method will help you to access any collection methods and you can easily drop the collection.

Use this command to drop the collection:

> db.getCollection("Student Database").drop()
true

Here in output, It displays true which means you successfully drop the collection.

Note that, If the collection does not exist in the database then MongoDB creates it implicitly as part of write operations.

Read: How to check if MongoDB is installed

MongoDB drop collection by name

In this topic, you will learn how to drop collections by name. First, you have to access the collection which you want to drop by using the getCollection() method then drop the collection using the drop() method.

Example:

You have to follow the steps to drop the collection by name:

  • Check all the database you have by using command show databases
  • Use particular database in which you want to drop the collection
  • The show collections command will show you all the collections inside the database
  • The db.getCollection(“Student”).drop() command will drop the collection and here Student is the collection inside the Company database.
MongoDB drop collection by name
MongoDB drop collection by name
  • After dropping the collection it generate the output as true, means collection is successfully dropped from the database.

Read: How to create a new database in MongoDB

MongoDB drop collection not working

In this problem, MongoDB is not deleting the collection or not working and this problem occurred while accessing the collection. This is a bug when a collection contains the characters like _,, and space.

This type of name for collections is acceptable, but it causes a problem when we use it in the shell.

To solve this problem you have to use the getCollection() method to access the collection and after that, you can easily drop it.

Example:

> db.getCollection("_register").drop()
true

In this example, First, we access the _register collection by using the getCollection() method and then drop it by using the drop() method. In the output, true means collection drops successfully.

Read MongoDB nested query

MongoDB drop collection using python

In this topic, you will learn how to drop the collection using python. For using MongoDB in python you have to use the pymongo library.

Example:

In this example, we insert some documents into the collection and after that using drop() method we drop the collection.

MongoDB drop collection using python
MongoDB drop collection by using python

Here, we import the pymongo library for making the connection between Python and MongoDB. After that use class MongoClient to start the MongoDB server.

We create a database and collection respectively name as organization and Student. You can use any variable to store the database and collection name.

Next, we store some documents into the collection by using the insert_many() method and in the last drop() method that is used to drop the collection.

Note that, The reason behind inserting some documents into the collection, So that you can easily understand how to insert multiple documents into the collection using python. You can directly drop the collection by using the drop() method.

In this topic, you learned how to drop the collection using python with the help of an example.

You may also like reading the following articles:

In this tutorial, we have learned “How to drop collection in MongoDB database” using different approaches with examples. These are the following topics that we covered in this tutorial

  • What is drop collection in MongoDB?
  • MongoDB drop collection
  • MongoDB drop collection command
  • MongoDB drop collection using MongoDB compass
  • MongoDB drop collection if exists
  • MongoDB drop collection if empty
  • MongoDB drop collection by using shell
  • MongoDB drop collection which has space in between the name
  • MongoDB drop collection by name
  • MongoDB drop collection not working
  • MongoDB drop collection using python