首页 > 解决方案 > 删除除其中一些之外的所有集合

问题描述

我想在所有集合上运行删除脚本,除了其中一些。

不想删除的整个集合仅来自一个(即 XYZ)客户端,因此每次来自 XYZ 客户端集合的集合都会增加。这就是为什么我无法使用以下脚本修复删除。因为每次我都需要提到那里增加的收藏品。

    // making variable for date before 93 days
var monthdate = new Date();
monthdate.setDate(monthdate.getDate() - 93);

//Date format should be : '2019/09/16'  (yyyy/MM/dd)

function objectIdWithTimestamp(timestamp) {
    // Convert string date to Date object (otherwise assume timestamp is a date)
    if (typeof(timestamp) == 'string') {
        timestamp = new Date(timestamp);
    }
    // Convert date object to hex seconds since Unix epoch
    var hexSeconds = Math.floor(timestamp/1000).toString(16);
    // Create an ObjectId with that hex timestamp
    var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
    return constructedObjectId
}
db.getCollectionNames().forEach(function(c) {
    if(c.match("AggBalances_") && 
    (!c.endsWith("AggBalances_910")) &&
    (!c.endsWith("AggBalances_934")) &&
    (!c.endsWith("AggBalances_966")) &&
    (!c.endsWith("AggBalances_981")) &&
    (!c.endsWith("AggBalances_999")) &&
    (!c.endsWith("AggBalances_1045")) &&
    (!c.endsWith("AggBalances_1073")) &&
    (!c.endsWith("AggBalances_1094")) &&
    (!c.endsWith("AggBalances_1105")) &&
    (!c.endsWith("AggBalances_1068")) 
    ) {    
   db.getCollection(c).remove({ _id: { $lt: objectIdWithTimestamp(monthdate) } })
        print(c);
    }
    else if(c.match("RecBalance_") &&
    (!c.endsWith("RecBalance_910")) &&
    (!c.endsWith("RecBalance_934")) &&
    (!c.endsWith("RecBalance_966")) &&
    (!c.endsWith("RecBalance_981")) &&
    (!c.endsWith("RecBalance_999")) &&
    (!c.endsWith("RecBalance_1045")) &&
    (!c.endsWith("RecBalance_1073")) &&
    (!c.endsWith("RecBalance_1094")) &&
    (!c.endsWith("RecBalance_1105")) &&
    (!c.endsWith("RecBalance_1068"))
    ) { 
   db.getCollection(c).remove({ _id: { $lt: objectIdWithTimestamp(monthdate) } })
        print(c);
    }
  });

@jiri 你从哪里得到你想要保持不变的集合的名称?

var setupArr = [];

db.setup.aggregate([
                     { $match: { "ClientID": "5c1cf5e29bbee71ac083b312" } }, // XYZ client Setup codes
                     { $project: { "SetupCode": 1} }
                     ]).forEach(function(user){
//print(user.SetupCode);
setupArr.push(user.SetupCode)
});

print(setupArr) 

从这里我得到了这些 collections_setupcodes 的名称,但无法使用这个(setupArr)数组跳过它们。

您可以动态加载名称吗?

因为我是一名 DBA,并且对脚本的了解很少,这就是我提出这个查询的原因。

你能把一个客户的数据保存到另一个数据库吗?

没有它的产品。

标签: javascriptmongodb

解决方案


我没有运行代码,但我认为这应该为您指明正确的方向:

const codes = db.setup.distinct("SetupCode", {
  ClientID: "5c1cf5e29bbee71ac083b312"
});

db.getCollectionNames()
  .filter(function(name) {
    // Does it end with AggBalance or RecBalance and a number?
    const match = name.match(".*(?:AggBalance|RecBalance)_([0-9]+)$");

    if (!match)
      return true;

    // Convert the match to an actual number and see if it's present in the
    // array
    const code = parseInt(match[1], 10);
    return !codes.includes(code);
  })
  .forEach(function(name) {
    print("About to remove from", name);
    // Test it first!
    // db.getCollection(name).remove({
    //   _id: {
    //     $lt: objectIdWithTimestamp(monthdate)
    //   }
    // });
  });

推荐阅读