简体   繁体   English

删除所有集合,除了其中的一些

[英]Remove all collections except some of them

I want to run remove script on all collection except some of them. 我想在除其中一些以外的所有集合上运行删除脚本。

The whole collections which don't want remove are from only one(ie XYZ) client so every time the collections from XYZ client's collections will increase. 不想删除的整个集合仅来自一个(即XYZ)客户端,因此每次XYZ客户端的集合中的集合都会增加。 thats why I am not able to fix the removal with below script. 这就是为什么我无法使用以下脚本修复删除的原因。 because every time I need to mention increased collections over there. 因为每次我需要提及那边增加的收藏品时。

    // 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 Where do you get the names of the collections that you want to leave untouched? @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) 

from here I am getting these name of collections_setupcodes but not able to skip them using this ( setupArr ) array. 从这里,我得到这些collections_setupcodes的名称,但无法使用此( setupArr )数组跳过它们。

Could you load the names dynamically? 您可以动态加载名称吗?

as I am a DBA and very less knowledge of scripting thats why I have raised this query. 因为我是一名DBA,对脚本的了解很少,所以我提出了这个查询。

Could you save the one client's data to a different database? 您可以将一个客户的数据保存到另一个数据库中吗?

No its PROD. 没有它的PROD。

I'm not running the code but I think this should point you to the right direction: 我没有运行代码,但是我认为这应该为您指明正确的方向:

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)
    //   }
    // });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM