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.
1 | 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.
1 2 | 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.
1 2 | 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.
1 | db.<collection_name>.insert(<document in JSON format>) |
1 2 | 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.
1 2 | anonymous:demodb >show collections democollection |
Now, the demodb database contain one collection named as “democollection”. To delete this collection, use the following command.
db.
1 2 | anonymous:demodb >db.democollection.drop() true |
Use “show collections” command once again to verify.
1 2 | 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:
1 | npm install mongodb --save |
To drop a collection the javascript code will look something like this:
1 | 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
1 2 3 4 5 6 | 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:
1 | dbo.collection("democollection").drop(function(err, delOK) { ... |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started