Let’s dive in.
Login to MongoDB Server
The first step is to login into your MongoDB server. If you are on your local machine, ensure the MongoDB server is up and running.
Run the command:
The command above should connect to your local Mongo server and drop you into an interactive shell.
Show Databases
The next step is to locate the database you wish to remove from the server. You can view all the databases in a server by running the command:
This command should output all the databases in the server and their corresponding disk size, as shown in the output below:
admin 40.00 KiB
config 48.00 KiB
customers 1.27 MiB
film 236.00 KiB
local 76.00 KiB
store 88.00 KiB
test 244.00 KiB
Switch to the Target Database
Once you have located the database you wish to remove, use the USE command to switch to that database.
For example, suppose we wish to remove the film database as shown in the output command; we can switch to that db with the command:
The command will switch to the specified database allowing you to execute commands on that database,
Mongo Shell Delete Database Using the dropDatabase() Method
MongoDB provides us with the db.dropDatabase() command that allows you to drop an existing database from the server. Keep in mind that this command will remove the currently selected database; otherwise, the command will remove the default “test” database.
For example, to remove the film database, run the command:
{ ok: 1, dropped: 'film' }
The command will return the status and the name of the database removed.
Conclusion
Through this tutorial, you learned how to use Mongo Shell commands to remove an existing database from a MongoDB server.
Thanks for reading!!