MongoDB Drop a Collection Tutorial with Example

Last updated on: by Digamber
In this mongoDB tutorial, we are going to understand how to drop a collection in MongoDB. In order to drop a collection from mongoDB, we need to connect to mognoDB database. We’ll be using the following method to delete a collection.

db.collection.drop()

The MongoDB is a well-known cloud-based database, and it provides various methods to deal with the different kind of situation related to the database. The db.collection.drop() method removes a collection from the database only when the collection exists. It works on boolean pattern and it returns true when the collection exists; else it returns to false.

Here, important thing to remember is, when you drop a collection from the data set, then all the related indexes are also be dropped. To maintain the data collection’s indexes, we might use the db.collection.remove() method. The remove() method doesn’t remove the indexes but removes a collection. To know more about mongoDB remove() method follow this tutorial on
removing a collection from mongoDB.

Getting Started

Go through this MEAN stack tutorial: MEAN stack CRUD app.

You can clone MEAN stack app from GitHub and run into your local machine. This way you will be able to play with mongoDB from your local machine.

Once you are done cloning the above Git repo, follow the given below steps:

How to start the app:

Start Front-end
Run npm install
Run ng serve to run the angular app

Starting MEAN Stack Backend Server
Run cd backend to get into the backend folder
Run nodemon command in other terminal
Run mongod command in other terminal

MongoDB Database URL

We are going to use mongoDB shell to manage the mongoDB collection. In our MEAN stack app we have created the `students` collection. We will be needing mongoDB database URL to do play with collection.

Go to backend > database > db.js, copy the mongoDB URL, it will help us to get into the mongoDB shell:

module.exports = {
  db: 'mongodb://localhost:27017/angular8mean'
}

The database name angular8mean.

Getting Inside mongoDB Shell

Now, It’s time to drop the collection using mongoDB shell locally. You can also follow the same process on remote mongoDB server.

Go to `backend` folder in our MEAN stack app:

cd backend

Copy your mongoDB URL and paste it along with mongo command in the terminal, if you are managing mongoDB on server then you’ll pass the mongoDB URL:

mongo mongodb://localhost:27017/mongodbname

Show MondoDB Databases

Run following command to checkout the database list:

$ show databases
# angular8mean

Run command to use the mongoDB database:

use angular8mean
use angular8mean
# switched to db angular8mean

Show MondoDB Collection

Run command to checkOUT the existing MongoDB collection, however we’ve already created the students collection.

$ show collections
students

View the total items in a collection:

db.students.find( {} )

After running the above command, your collections items will be shown in the terminal, now you can drop a collection by using the below process.

{ "_id" : ObjectId("5d035b580c2ad404f24a9e3f"), "subjects" : [ { "name" : "english" }, { "name" : "hindi" } ], "student_name" : "Digamber", "student_email" : "digambersingh126@gmail.com", "section" : "E", "dob" : ISODate("2019-06-26T00:00:00Z"), "gender" : "Male", "__v" : 0 }
{ "_id" : ObjectId("5d0cbe57c1d72e07b8a78a40"), "subjects" : [ { "name" : "english" }, { "name" : "french" } ], "student_name" : "Jenell Jarrell", "student_email" : "jenell@gmail.com", "section" : "B", "dob" : ISODate("2019-06-28T00:00:00Z"), "gender" : "Male", "__v" : 0 }
{ "_id" : ObjectId("5d0cbe7cc1d72e07b8a78a41"), "subjects" : [ { "name" : "french" } ], "student_name" : "Dodie Doiron", "student_email" : "dodie@hotmail.com", "section" : "B", "dob" : ISODate("2019-06-25T00:00:00Z"), "gender" : "Male", "__v" : 0 }
{ "_id" : ObjectId("5d0cbe9ec1d72e07b8a78a42"), "subjects" : [ { "name" : "mexican" }, { "name" : "english" } ], "student_name" : "Trish Tyndall", "student_email" : "trish@yahoo.com", "section" : "B", "dob" : ISODate("2019-06-21T00:00:00Z"), "gender" : "Female", "__v" : 0 }

MongoDB Drop A Collection

Now we are all set to drop the collection in mongoDB, run the command along with your collection name.

db.students.drop()

Following will be the output, when mongoDB collection successfully removed from the database:

true

It will return true if mongoDB collection is removed from the database. If you check collection again then it won’t show your mongoDB collection:

show collections
> ...

Drop Non-Existing MongoDB Collection

In this final step, we will check if we try to remove the non-existing mongoDB collection.

db.employees.drop()

Following will be the output when we remove non-existing collection in MongoDB.

false

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.