【问题标题】:MongoDB shell: how to remove all collections except listMongoDB shell:如何删除列表以外的所有集合
【发布时间】:2023-01-13 18:01:54
【问题描述】:

我想删除除列表之外的所有集合。

db.getCollectionNames().forEach(function(n){db[n].remove({})});

将删除所有集合。

db.getCollectionNames().filter(function(collection){return! /^((keepthisone)|(andthisone)|(alsokeepthisone))$/.test(collection)});

将列出所有收藏,除了我想保留的那些。

我如何将两者结合起来?

db.getCollectionNames().filter(function(collection){return! /^((keepthisone)|(andthisone)|(alsokeepthisone))$/.test(collection)}).forEach(function(n){db[n].remove({})});

什么也没做。

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    我会 map 然后 drop 项目:

    db.getCollectionNames().filter(function(collection) {
        return !/^((keepthisone)|(andthisone)|(alsokeepthisone))$/.test(collection);
    }).map(function(n){
        return db.getCollection(n);
    }).forEach(function(collection){
        collection.drop();
    });
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      相关资源
      最近更新 更多