MongoDB 删除每个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6376436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:07:36  来源:igfitidea点击:

MongoDB drop every database

mongodb

提问by John

I would like to know if there're a command to drop every databases from my MongoDB?

我想知道是否有命令可以从我的 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();'

回答by ALoR

you can create a javascript loop that do the job and then execute it in the mongoconsole.

您可以创建一个 javascript 循环来完成这项工作,然后在 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:

保存到 dropall.js 然后执行:

mongo dropall.js

回答by kev

Try this command:

试试这个命令:

mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})'

回答by Andreas

You can also do this with a simple mongo command:

你也可以用一个简单的 mongo 命令来做到这一点:

db.adminCommand("listDatabases").databases.forEach( function (d) {
    if (d.name != "local" && d.name != "admin"  && d.name != "apiomat"  && d.name != "config")
        db.getSiblingDB(d.name).dropDatabase();
 })

回答by btiernay

Adding to @ALoR's answer, for convenience you can put the following in ~/.mongorc.js

添加到@ALoR 的答案中,为方便起见,您可以将以下内容放入~/.mongorc.js

function dropDatabases(){
    var mongo = db.getMongo();

    var dbNames = mongo.getDBNames();
    for (var i = 0; i < dbNames.length; i++) {
        var db = mongo.getDB( dbNames[i] );

        print( "Dropping database " + db.getName() + "..." );
        db.dropDatabase();
    }
}

Then at the mongo shell you can simply do

然后在 mongo shell 你可以简单地做

dropDatabases()

From the docs:

从文档:

Mongo will read the .mongorc.js file from the home directory of the user invoking mongo. In the file, users can define variables, customize the mongo shell prompt, or update information that they would like updated every time they launch a shell.

Mongo 将从调用 mongo 的用户的主目录中读取 .mongorc.js 文件。在该文件中,用户可以定义变量、自定义 mongo shell 提示或更新他们每次启动 shell 时都希望更新的信息。

回答by Gas

Save this to drop_all_dbs.js:

将其保存到 drop_all_dbs.js:

var databases = db.getMongo().getDBNames()
for(var i in databases){
    db = db.getMongo().getDB( databases[i] );
    if(db.getName() == "admin" || db.getName() == "local"){
        print("skipping db " + db.getName())
        continue
    }
    print( "dropping db " + db.getName() );
    db.dropDatabase();
}

Now you can execute:

现在你可以执行:

mongo drop_all_dbs.js

and all databases (except for admin and local) will be dropped.

并且所有数据库(除了 admin 和 local)都将被删除。

This answer is a copy of ALoR's one, just fix drop of system dbs

这个答案是 ALoR 的副本,只需修复系统数据库的丢失

回答by Andrew Orsich

You can do it easy through c# official driver:

您可以通过 c# 官方驱动程序轻松完成:

var _mongoServer = MongoServer.Create("mongodb://localhost:27020");

var names = _mongoServer.GetDatabaseNames();
foreach (var name in names)
{
   _mongoServer.DropDatabase(name);
}

回答by vu le

var mongo = db.getMongo(); mongo.getDBNames().filter(n => n != 'admin' && n != 'local' && n != 'config').forEach(function (dbname) { var db = mongo.getDB(dbname); db.dropDatabase(); });

var mongo = db.getMongo(); mongo.getDBNames().filter(n => n != 'admin' && n != 'local' && n != 'config').forEach(function (dbname) { var db = mongo.getDB(dbname); db.dropDatabase(); });

This one is safe to copy and execute on mongoshell. Credits to all answers above. Just exclude 'config' database as well.

这个可以安全地在 mongoshell 上复制和执行。归功于以上所有答案。也只需排除“配置”数据库。

回答by Pedro José Piquero Plaza

It is as easy as

就这么简单

mongo --eval 'db.dropDatabase()'

Or, you can start a mongo session on your terminal and write

或者,您可以在终端上启动 mongo 会话并编写

db.dropDatabase()

Which is exactly the same.

这是完全一样的。