MongoDB:删除名称与字符串匹配的所有集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12844531/
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-13 15:57:10  来源:igfitidea点击:

MongoDB: Removing all collections whose name matches a string

mongodb

提问by Zane Claes

I've restructured my database a bit, and no longer have need for certain collections. However, there are far too many of them to remove by-hand (thousands, actually). Each of the collections in question begins with "cache_" and contains a couple of indexes which I'd like to make sure are completely cleaned up.

我对我的数据库进行了一些重组,不再需要某些集合。但是,其中有太多无法手动删除(实际上有数千个)。每个有问题的集合都以“cache_”开头,并包含几个索引,我想确保它们被完全清理。

I'm trying to understand how to use the mongo shell in order to loop over all collection names and remove those collections which begin with "cache_". Per the Queries & Cursors documentation, I understand how to loop over documents within a collection, but not how to use the MongoDB shell to loop through the collections in the database.

我试图了解如何使用 mongo shell 来遍历所有集合名称并删除那些以“cache_”开头的集合。根据Queries & Cursors 文档,我了解如何循环访问集合中的文档,但不了解如何使用 MongoDB shell 循环访问数据库中的集合。

In pseudo-code, this is what I need:

在伪代码中,这就是我需要的:

var all_collections = show collections
for(var collection in all_collections)
    if(collection.name.indexOf('cache_')==0)
        collection.drop()

FWIW, I've done searching for "mongodb loop through collection names" etc. and have not found anything, but maybe I sux at teh googlez =P

FWIW,我已经搜索过“mongodb 循环通过集合名称”等,但没有找到任何东西,但也许我在 googlez =P

On a related note... after doing this degree of restructuring, should I be doing a db.repairDatabase()or anything of the sort in order to make sure the dropped indexes etc. are all nice and clean?

在相关说明中...在进行这种程度的重组后,我是否应该做类似的db.repairDatabase()事情以确保删除的索引等都很好且干净?

Thanks.

谢谢。

回答by driangle

Use db.getCollectionNames()to get the all the collections and store them in an array.

使用db.getCollectionNames()来获取所有集合,并将它们存储在数组中。

var collectionNames = db.getCollectionNames();
for(var i = 0, len = collectionNames.length; i < len ; i++){
    var collectionName = collectionNames[i];
    if(collectionName.indexOf('cache_') == 0){
        db[collectionName].drop()
    }
}

回答by Nishant

Just to add another answer found on mongodb mailing list

只是为了添加在 mongodb 邮件列表中找到的另一个答案

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

You probably want to not drop the local/config/admin dbs as I've listed above.

您可能不想删除上面列出的 local/config/admin dbs。

回答by chris mathias

The following worked great for me to remove the collections created by map reduce jobs. In my case they were deleted automatically on the local setup, but for whatever reason stuck around on the server.

以下对我来说非常有用,可以删除由 map reduce 作业创建的集合。在我的情况下,它们在本地设置中被自动删除,但由于某种原因停留在服务器上。

db.getCollectionNames().forEach( function (d) {
  if (d != "collectionICareAbout" && d != "system.indexes" && d != "system.users") {  
        print("dropping: " + d);
        db[d].drop();
  }
})

This is similar to Nishant's answer, but the difference is I don't have admin rights and cannot invoke db.adminCommand (hosted mongo instance).

这类似于 Nishant 的回答,但不同之处在于我没有管理员权限并且无法调用 db.adminCommand(托管的 mongo 实例)。