How do I drop a MongoDB database from the command line ?
How do I drop a MongoDB database from the command line ?
We can use the following code:
mongo <dbname> --eval "db.dropDatabase()"
Further details on scripting the shell from the below link:
https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#scripting
This is the excellent way to do it from mongodb console:
> use mydb; > db.dropDatabase();
Here another, we can stop mongod and delete the data files from your data directory, then restart.
Tips: we can also move the data files to a subfolder, and delete them if we are sure you no longer need them.
Here there is no need of heredocs or eval,mongo itself can act as an interpreter.
#!/usr/bin/env mongo var db = new Mongo().getDB("someDatabase"); db.dropDatabase();
Create the file executable and run it.