How to drop a collection in MongoDB?


To drop a collection in MongoDB, you need to use drop() command. The syntax is as follows:

db.yourCollectionName.drop();

The above syntax returns true or false. It returns true if the collection is dropped successfully otherwise false.

Let us first display all collection names from MongoDB. Here, we have a database ‘sample’ that contains some collections. First you need to switch to the ‘sample’ database. The query is as follows:

> use sample;
switched to db sample

Now display all collection names with the help of show command. The query is as follows:

> show collections;

The following is the output:

bookInformation
userInformation

Above, you can see we have the following two collections:

  • bookInformation

  • userInformation

Now let us drop the collection name ‘bookInformation’. The query is as follows:

> db.bookInformation.drop();

The following is the output displaying true:

true

The result true specify that we have dropped the collection successfully. Now you can display all the collection names with the help of show command to verify. The query is as follows:

> show collections;

The following is the output:

userInformation

Look at the above sample output, there is no collection with the name ‘bookInformation.’

Updated on: 30-Jul-2019

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements