Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何删除MongoDB中的所有数据库?_Database_Mongodb_Nosql - Fatal编程技术网

Database 如何删除MongoDB中的所有数据库?

Database 如何删除MongoDB中的所有数据库?,database,mongodb,nosql,Database,Mongodb,Nosql,我的MongoDB中有一个数据库列表。如何删除除本地、管理和配置之外的所有数据库? 您可以在mongo shell中使用getDBNames方法 必须从中调用此方法。不幸的是,我认为getDBNames方法没有文档记录 获取数据库名称后,您可以使用以下方法循环访问它们以删除不需要的名称: Mongo().getDBNames().forEach(function(x) { // loop through all the database names if (['admin', 'conf

我的MongoDB中有一个数据库列表。如何删除除本地、管理和配置之外的所有数据库?

您可以在mongo shell中使用getDBNames方法

必须从中调用此方法。不幸的是,我认为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
您可以在mongo shell中使用getDBNames方法

必须从中调用此方法。不幸的是,我认为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

我用这个解决方案修复了这个问题

我在mongo shell中运行了此代码

var dbs = db.getMongo().getDBNames()
for(var i in dbs){
    db = db.getMongo().getDB( dbs[i] );
    if (db.getName() !== 'admin' && db.getName() !== 'local') 
    {
        print( "dropping db " + db.getName() );  
        db.dropDatabase();
    }
}

我用这个解决方案修复了这个问题

我在mongo shell中运行了此代码

var dbs = db.getMongo().getDBNames()
for(var i in dbs){
    db = db.getMongo().getDB( dbs[i] );
    if (db.getName() !== 'admin' && db.getName() !== 'local') 
    {
        print( "dropping db " + db.getName() );  
        db.dropDatabase();
    }
}

有趣的似乎没有方法以代码友好的方式枚举数据库或其名称。有一个用于集合,但不用于数据库。如果您知道名称,那么它很简单:db.getSiblingDB'Marks.dropDatabase。所以你可以简单地硬编码这些名字。检查一下[@SergioTulentsev实际上有一种方法可以做到这一点,只是没有正式的文档记录。请看下面我的答案。有趣的是,似乎没有方法以代码友好的方式枚举数据库或其名称。有一种方法用于集合,但不用于数据库。如果你知道名称,它很简单:db.getSiblingDB'Marks.dropDatabase你可以简单地对这些名字进行硬编码。检查一下[@SergioTulentsev实际上有一种方法可以做到这一点,只是没有正式的文档记录。请看我下面的答案。这是一个救命的人。谢谢。♥这是个救命的人,谢谢你。♥