MongoDB Remove a Collection Tutorial

Last updated on by Digamber
In this mongoDB tutorial, we are going to learn how to remove a collection in MongoDB. In order to delete a collection from mongoDB, we required to connect to the database. In this tutorial, we will set up a MEAN stack app by taking a clone from this GitHub repo. But before that let’s understand about the MongoDB remove method.

The MongoDB Remove Method:


The MongoDB is a cloud-based NoSQL database based on document and collection model. The MongoDB database offers wide array of built-in methods. Which help developers to sort-out everyday database related problems. The db.collection.remove() method returns `true` when the collection exist and returns `false` when the collection is not available.

If you ask me why we use it so here is my answer!

When you required to remove a collection from the mongoDB database, but don’t want to lose the collection indexes. It help us to keep the collection’s indexes, then we use db.collection.remove() method. The mongoDB remove() method doesn’t remove the indexes but removes a collection.

Getting Started

Follow every step to setup a MEAN stack app with MongoDB on local system: MEAN Stack CRUD App and you can get the Git repo here.

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

The mongoDB URL

We are using mongoDB shell to access the collection from the local system in this tutorial. You can get the `students` collection however you can change the collection name if you want to.

Go inside backend > database > db.js, copy the local mongoDB URL:

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

Our database name is: angular8mean.

Working with mongoDB Shell

Let us remove a collection using mongoDB in our basic MEAN stack app. If using the above Git repo then get inside the app’s `backend` folder and type the below command in the the terminal.

cd backend

Paste the following command in the terminal and don’t forget to replace the mongoDB database name with your database name.

mongo mongodb://localhost:27017/mongodbname

Check Databases in MongoDB

Run command to check the total number of database in your local system, although we are using `angular8mean`.

show databases

# angular8mean
# database2
# database3
# database4

Use MongoDB Database

Run below command to select mongoDB database using MongoDB shell:

use angular8mean
use angular8mean

# switched to db angular8mean

Check MondoDB Collection

Run command to check the total number of existing collection in the mongoDB. In our MEAN stack app i have already created the `students` collection.

$ show collections

See total number of items in a collection using the given below command:

db.students.find( {} )

After using the above cmd, your collections items will be shown on mongoDB shell.

{ "_id" : ObjectId("5d035b580c2ad404f24a9e3f"), "subjects" : [ { "name" : "english" }, { "name" : "hindi" } ], "student_name" : "Digamber", "student_email" : "", "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" : "", "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" : "", "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" : "", "section" : "B", "dob" : ISODate("2019-06-21T00:00:00Z"), "gender" : "Female", "__v" : 0 }

MongoDB Remove Collection

Run the command to remove the collection in mongoDB.


The output:


It will return true if mongoDB collection is removed from the database.

show collections
> ...

Remove Non-Existing MongoDB Collection

Finally, we will see what will happen if we try to remove the non-existing mongoDB collection.


The output.