MongoDb Tutorial

Day 11 : Drop Collection in MongoDB








Mongodb tutorial series

Overview

In this part of the Learn Mongo Series, we will learn how to drop a collection in mongodb. We will drop the details collection from the database.

Let's drop

We can drop a collection using the db.collection.drop() command. The syntax is db.<Collection_name>.drop() . Let's start dropping collections.

  • Step 1 - List the collections: firstly list all the collections using the show collections command as shown below :

    										
    >use students
    switched to db students
    > show collections
    name
    roll
    details
    rollNumber
    system.indexes
    >
    										
    									

  • Step 2 - Drop the collection: We can drop the collection using db.collection.drop() command. We are deleting the details collection from students db as shown below :

    										
    >db.details.drop()
    true
    >
    										
    									

  • Step 3 - List collections again : List all the collections again to check the successful deletion of details collection as shown below :

    										
    > show collections
    name
    roll
    rollNumber
    system.indexes
    >
    										
    									

  • Hurray !! Mission Accomplished. We have successfully deleted the mongodb collection.

Summary

In this part of learn mongo series , we learned about how we can drop a collection in mongodb. We learned the following commands of mongodb :

  1. use : This command is use to either create a new database or switch to an already existing database.
  2. show collections : This command is used to list all the collections available in the mongodb databases on the console.
  3. db.<Collection_name>.drop() : This command is used to drop the collection.