Mongodb shell 掉落 collections 外卡

Mongodb shell drop collections with wild card

我有一堆 collection 具有相同的前缀,例如data_user_1、data_user_2 等 中间还有一些 collection 我想保留并跳过。

所以我寻找

的解决方案

a) 删除所有排除特定 b) 删除所有 data_user_*

我搜索了 docs,但似乎没有允许任何这些操作的参数。 谢谢

遗憾的是没有这样的选项。

  1. 您可以使用 getCollectionNames

    检索所有可用的集合
  2. 然后您可以检查 include/exclude 个场景。

  3. 当条件匹配时,可以对其执行drop

我放弃搜索了,好像没有什么“舒服”的方法。所以我写了一个简短的脚本来满足要求(而且也很舒服^^)。

db.getCollectionNames().forEach(function(collname){
if(collname.includes('data_user')){ 
 print('delete collection: ' + collname); 
 db.getCollection(collname).drop()}
});

希望这对其他人也有帮助。