To drop a MongoDB database from the command line, use the following syntax:
mongo yourDatabaseName --eval "db.dropDatabase()"
To understand the above syntax, let us display all the database from MongoDB. The query is as follows −
> show dbs;
The following is the output −
StudentTracker 0.000GB admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB test 0.003GB
Drop the database with the name ‘StudentTracker’. The query is as follows to drop a database from command line −
C:\Program Files\MongoDB\Server\4.0\bin>mongo StudentTracker --eval "db.dropDatabase()"
The following is the output −
MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/StudentTracker?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("afc34e93-b4c0-46f0-85bd-b80ed17b8c11") } MongoDB server version: 4.0.5 { "dropped" : "StudentTracker", "ok" : 1 }
Now check the database has been dropped or not. The query is as follows −
> show dbs
The following is the output −
admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB test 0.003GB
Look at the sample output, the database ‘StudentTracker’ has been dropped.