MongoDB Delete Collection Command Line

Introduction

Mongodb is documented oriented database. It is the leading Nosql database which works on the concept of collections and documents. It is a high performance database which is easy to use. Commands used in creating database and collections are very simple as compared to other databases. Creating, inserting, updating and deleting is very easy in mongodb. In this article, I am going to show how to delete a collection.

Note: If you don’t need an explanation and just want to look at some code samples you can proceed to the end of the article for the Just the Code section

A collection is a storage for documents. To delete a collection, first we need to create one. First, switch to a database. Use the following command.

use <database_name>

This command will switch to the database whose name is entered. If there is no database of that particular name, then it will be created automatically.

anonymous:demoDatabase >use demodb
switched to db demodb

Database named as “demodb” is created.

The following command will show the collections present in the selected database.

anonymous:demodb >show collections
anonymous:demodb >

As of now, demodb database does not contain any collections.

To create a new collection, use the following command.

db.<collection_name>.insert(<document in JSON format>)
anonymous:demodb >db.democollection.insert({key: "value"})
WriteResult({ "nInserted" : 1 })

It will show a message declaring that one collection was created. Use the show collection command once again.

anonymous:demodb >show collections
democollection

Now, the demodb database contain one collection named as “democollection”. To delete this collection, use the following command.

db..drop()

is the name of collection you want to delete. After executing this command, “true” will be printed on screen. This indicates that the selected collection was deleted successfully. If it prints “false”, it means there is no collection with the name you specified.

anonymous:demodb >db.democollection.drop()
true

Use “show collections” command once again to verify.

anonymous:demodb >show collections
anonymous:demodb >_

How to Delete a Collection with Drivers

MongoDB provides a driver out there for almost every popular programming language and there are third party libraries as well. We’ll quickly go over how the code might look for deleting a collection from one of the drivers.

We’ll use the Node.js drivers that MongoDB provides. If you don’t already have them installed you’ll need to install them using something like npm:

npm install mongodb --save

To drop a collection the javascript code will look something like this:

  dbo.collection("democollection").drop(function(err, delOK) { ...

Please check out the documentation for your specific driver.

Conclusion

In this short tutorial we showed you how to delete a collection from MongoDB using the command line. We demonstrated an example of doing it ourselves. We also discussed how you might doing it using the drivers for whatever language you might be using in your application.

Just the Code

anonymous:demoDatabase >use demodb
switched to db demodb
anonymous:demodb >db.democollection.insert({key: "value"})
WriteResult({ "nInserted" : 1 })
anonymous:demodb >db.democollection.drop()
true

If you’re using the official Node.js drivers:

  dbo.collection("democollection").drop(function(err, delOK) { ...

Pilot the ObjectRocket Platform Free!

Try Fully-Managed Redis,
MongoDB & Elasticsearch

Get Started

OR

Try CockroachDB
in Beta

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.