How to Drop a MongoDB Database?

By Rohit Sharma

Updated on Oct 27, 2024 | 6 min read | 9.25K+ views

Share:

MongoDB is the most popular NoSQL database among both enterprise and startups, and the fact that it is highly scalable makes it perfectly suited for current web-apps, which need to scale once the user base increases. MongoDB is different from traditional relational databases because it uses json like objects to store data, instead of tables in relational databases.

In this post, we will learn to drop a MongoDB database, using MongoDB Drop Database command. Dropping a database means dropping all the collections (tables in MongoDB) in it, along with the indexes. If you don’t want to drop the entire database, you can drop individual collections.

Prerequisites

We are using Windows 10 in this tutorial. Please make sure you have downloaded the MongoDB Community Server and installed it. It is a very easy setup and you will find a lot of good articles on the internet to guide you through this process. Do make sure that you have added it in the Environment variable in your PC. We have also created an example database, which contains a collection named products.

Overview

Dropping a database is quite simple and can be done with three methods from the command line and also by any GUI (Graphical User Interface) tool, which is used to connect with the running MongoDB instance.

To delete a MongoDB database through the command line, we mainly use db.dropDatabase(). We will first understand how to do this. 

Dropping with dropDatabase Command

We can drop the database through dropDatabase command. I am connected to my local mongoDB instance from the windows terminal, by giving the mongo command.

> mongo

After that to show all the databases in my MongoDB database, we will use the command show dbs command. It will show all our databases. Out of it I created the example database only.

> show dbs
admin    0.000GB
config   0.000GB
example  0.000GB
local    0.000GB

Now, to drop the example database, first we have to go inside it by using the use example command.

> use example
switched to db example

Now, we will use the dropDatabase command from within a db.runCommand(). Now, the runCommand runs the command in the context of the current database. This means it will run the dropDatabase command in the current database, which is seen in this example:

> db.runCommand( { dropDatabase: 1 } )
{ "dropped" : "example", "ok" : 1 }

As discussed, the earlier dropDatabase command accepts an optional writeConcern argument, and also a comment field. The syntax then is written as shown below.

{ dropDatabase: 1, writeConcern: <document>, comment: <any> }


Dropping with dropDatabase() Method

We can drop the database through db.dropDatabase() method also. It also removes the current database, deleting all associated files.

Once again connect to mongoDb database by giving the command mongo. After that we can check all the databases with show dbs command.

For db.dropDatabase() method also, we need to go inside the database that we are dropping, by using use <dbName> command.

Now, we will use the db.dropDatabase() command to drop the database, which is example in our case.

> db.dropDatabase()
{ "dropped" : "example", "ok" : 1 }
The db.dropDatabase() method also accepts an optional writeConcern argument. The syntax then becomes:
{ w: <value>, j: <boolean>, wtimeout: <number> }

Dropping with Unix Shell and Eval command

This method only works in a Unix environment like Ubuntu, Mac or WSL2 for Windows. Here, the mongo command with the database name can be used to connect to a specific database. For example, to connect to the example database, we can use the below command.

$ mongo example

Now, we can drop our example database with one line command, where we use the eval command followed by the JavaScript code that we wish MongoDB to execute. As seen earlier to drop the database, we use the db.dropDatabase() command. Now, we will wrap this method in printjson function, to get the output of the command.

$ mongo example --eval "printjson(db.dropDatabase())"
MongoDB shell version: 3.0.9
connecting to: example
{ "dropped" : "example", "ok" : 1 }

Dropping with MongoDB Compass

Now we can drop a database or delete MongoDB database very easily using a Graphical tool like MongoDB compass. Infact this tool was auto-downloaded when I had installed MongoDB on my Windows machine.

So, first open the MongoDB Compass and you will see the option to connect to a database. We want to connect to our local database, in which the hostname is localhost and port is 27017. These are the default values for all MongoDB connections, unless you changed it during installation.

Don’t change anything and click on the Connect button.

We are now connected to the MongoDB running on our localhost and we can see all the databases including example database. Next, hover over example database, which will show a delete button. Click on the delete button.

Data Science Courses to upskill

Explore Data Science Courses for Career Progression

background

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

We will be shown a pop-up where we have to write the name of our database, which is example in our case. Then click on the DROP DATABASE button, to MongoDB remove database.

After that it will take us back to the earlier screen and we can see the example database is dropped.

Enhance your expertise with our Software Development Free Courses. Explore the programs below to find your perfect fit.

Subscribe to upGrad's Newsletter

Join thousands of learners who receive useful tips

Promise we won't spam!

Elevate your expertise with our range of Popular Software Engineering Courses. Browse the programs below to discover your ideal fit.

Advance your in-demand software development skills with our top programs. Discover the right course for you below.

Explore popular articles related to software to enhance your knowledge. Browse the programs below to find your ideal match.

Conclusion

In this article, we have learned about the different MongoDB delete database commands. We have also learned about the write concern optional parameter, which can be passed to both db.dropDatabase() method and dropDatabase command. This parameter is mainly used in large database systems, which have several servers. 

We hope you found this article useful! Keep learning.

Frequently Asked Questions (FAQs)

1. What is Sharding in databases?

Sharding is a process of dividing a huge database table into smaller ones to improve performance. Each of these smaller tables is called a shard. These shards are replicated and distributed among multiple computers. It is necessary because, as the size of the dataset keeps growing larger, the computing power of a single machine wouldn't be sufficient. It helps in horizontal scaling as each machine has to scan only a few rows leading to efficient usage of resources. Sharding increases the read-write throughput, increases scalability, and makes data available. However, it also leads to higher complexity, higher query overhead, and increased infrastructure costs.

2. What are the advantages of using an RDBMS over MongoDB?

RDBMS stands for Relational Database Management System. It stores data in tables in rows and columns. It works on structured data and SQL is the language used for performing operations on the database. RDBMS works well with complex queries like joins, subqueries, nested queries, etc., which cannot be done using MongoDB. It supports secondary indices too. Relational databases are preferred when the number of tables is large and when referential integrity has to be maintained. Even though MongoDB also supports transaction management, relational systems that satisfy all the ACID properties are preferred as data integrity is maintained at every instant of time.

3. What are the use cases of MongoDB?

MongoDB is an open-source NoSQL database. It stores data in the form of collections which in turn store documents. Each document stores data in the form of key-value pairs. It is very fast, flexible, and supports sharding and horizontal scalability. MongoDB is used for content management as it supports both structured and unstructured data. It also has its applications in devices of the Internet of Things, wherein it optimises data storage techniques to store the data coming from multiple connected devices. MongoDB is also used for real-time analysis of data and is cost-effective too. It is also used in product data management, maintaining product catalogue, scaling and application mobility, and customer analytics.

Rohit Sharma

834 articles published

Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...

Speak with Data Science Expert

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

upGrad
new course

Certification

30 Weeks

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree

17 Months