MongoDB 3.2 ve daha yeni, in Mongo().getDBNames()içinde mongosunucudaki veritabanı adları kabuk irade çıkışı bir liste:
> Mongo().getDBNames()
[ "local", "test", "test2", "test3" ]
> show dbs
local 0.000GB
test 0.000GB
test2 0.000GB
test3 0.000GB
forEach()Sonra dizi üzerinde bir döngü dropDatabase()listelenen tüm veritabanlarını bırakmak için çağırabilir . İsteğe bağlı olarak, bırakmak istemediğiniz bazı önemli veritabanlarını atlamayı tercih edebilirsiniz. Örneğin:
Mongo().getDBNames().forEach(function(x) {
// Loop through all database names
if (['admin', 'config', 'local'].indexOf(x) < 0) {
// Drop if database is not admin, config, or local
Mongo().getDB(x).dropDatabase();
}
})
Örnek çalışma:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
test2 0.000GB
test3 0.000GB
> Mongo().getDBNames().forEach(function(x) {
... if (['admin', 'config', 'local'].indexOf(x) < 0) {
... Mongo().getDB(x).dropDatabase();
... }
... })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB