如何删除MongoDB中的所有数据库? [英] How to drop all databases in MongoDB?
本文介绍了如何删除MongoDB中的所有数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的MongoDB中有一个数据库列表。如何删除除local
、admin
、config
以外的所有数据库?
推荐答案
可以在mongo
外壳中使用getDBNames()
方法。
此方法必须从Mongo()
instance调用。遗憾的是,我认为没有记录getDBNames()
方法。
获得数据库名称后,您可以使用如下内容遍历它们以删除不需要的名称:
Mongo().getDBNames().forEach(function(x) {
// loop through all the database names
if (['admin', 'config', 'local'].indexOf(x) < 0) {
// drop database if it's not admin, config, or local
Mongo().getDB(x).dropDatabase();
}
})
例如:
> show dbs
admin 0.000GB
config 0.000GB
local 0.001GB
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.001GB
这篇关于如何删除MongoDB中的所有数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文