I would like to know if there're a command to drop every databases from my MongoDB?
I know if I want to drop only one datatable, I just need to type the name of the database like the code below but I dont want to have to specify it.
mongo DB_NAME --eval 'db.dropDatabase();'
Then you can use “show dbs” command for checking the list of Databases. Then select the database you want to delete using command “use databasename“. Then execute db. dropDatabase() command to drop an existing database.
The best way to do it is from the mongodb console: > use mydb; > db. dropDatabase(); Alternatively, you can stop mongod and delete the data files from your data directory, then restart.
To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.
you can create a javascript loop that do the job and then execute it in the mongoconsole.
var dbs = db.getMongo().getDBNames() for(var i in dbs){ db = db.getMongo().getDB( dbs[i] ); print( "dropping db " + db.getName() ); db.dropDatabase(); }
save it to dropall.js and then execute:
mongo dropall.js
Try this command:
mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With