MongoDB: Delete/Remove Database
In this tutorial, we will learn how to delete/remove a database, which consists of collections with documents inside them.
We consider the database books
which was created in one of our previous tutorial.
We first need to start MongoDB.
If you are using a Mac, open your terminal and start the Mongo Daemon first.
$ mongod
Next, open another new terminal (CTRL
+ N
) and start the Mongo Shell.
$ mongo
If you are using Windows, there is the /bin
directory you need to navigate to. Double-click on the Mongo Daemon mongod.exe
and next in the same directory, double-click on mongo.exe
. This is the Mongo Shell and you will be typing your commands from here.
To delete a MongoDB database via the Mongo Console, you first need to switch to the targeted database using the use
command.
Below we first switch to the database called books
(which was created earlier in one of our tutorials), and then we use the dropDatabase() method to remove it.
$ use books
$ db.dropDatabase()
The books
database is now removed.