如何在UPSERT语句中有条件地更新子属性?

时间:2017-08-24 03:34:22

标签: arangodb aql

我想通过以下条件更新属性regions

  • 如果regions中不存在区域R,则将新区域R推送到regions

  • 否则,如果区域R存在,则在accounts

  • 中更新该区域R的属性regions

因此,相同边缘没有重复区域,同一区域没有重复帐户。

这是我的AQL:

1     FOR t IN transactions
2       SORT t.accountId, t.recordedAt
3       COLLECT accountIds = t.accountId INTO g
4       FOR i IN 0..LENGTH(g)-2
5         LET region = g[i].t.region
6         LET doc = (i == 0)
7           ? [{"_from": "status/root", "_to": CONCAT("status/", g[0].t.status), "recordedAt": g[i].t.recordedAt, "region": region, "level": i, "accountId": accountIds}
8             ,{"_from": CONCAT("status/", g[i].t.status), "_to": LENGTH(g) > 1 ? CONCAT("status/", g[i+1].t.status) : null, "recordedAt": g[i+1].t.recordedAt, "region": region, "level": i + 1, "accountId": accountIds}]
9           : [{"_from": CONCAT("status/", g[i].t.status), "_to": LENGTH(g) > 1 ? CONCAT("status/", g[i+1].t.status) : null, "recordedAt": g[i+1].t.recordedAt, "region": region, "level": i + 1, "accountId": accountIds}]
10      FOR d IN doc
11        FILTER d._to != null
12        UPSERT {"_from": d._from, "_to": d._to, "level": d.level}
13        INSERT {"_from": d._from, "_to": d._to, "level": d.level, "regions": [{"name": d.region, "accounts": [{"id": d.accountId, "recordedAt": d.recordedAt}]}]}
14        UPDATE {
15          "regions": 
16             OLD.regions[*].name ANY == d.region
17             ? PUSH(OLD.regions, {"name": d.region, "accounts": PUSH(OLD.regions[* FILTER CURRENT.name == d.region].accounts[**], {"id": d.accountId, "recordedAt": d.recordedAt})}, true)
18             : PUSH(OLD.regions, {"name": d.region, "accounts": [{"id": d.accountId, "recordedAt": d.recordedAt}]})
19        }
20        IN patterns

实际结果:

{
    "_from": "status/root",
    "_to": "status/A",
    "level": 0,
    "regions": [
      {
        "name": "North",
        "accounts": [
          {
            "id": 1,
            "recordedAt": "2016-08-01T00:00:00.000Z"
          }
        ]
      },
      {
        "name": "South",
        "accounts": [
          {
            "id": 2,
            "recordedAt": "2012-05-01T00:00:00.000Z"
          }
        ]
      },
      {
        "name": "North",
        "accounts": [
          {
            "id": 1,
            "recordedAt": "2016-08-01T00:00:00.000Z"
          },
          {
            "id": 4,
            "recordedAt": "2017-04-01T00:00:00.000Z"
          }
        ]
      }
   ]
}

预期结果:

{
    "_from": "status/root",
    "_to": "status/A",
    "level": 0,
    "regions": [
      {
        "name": "North",
        "accounts": [
          {
            "id": 1,
            "recordedAt": "2016-08-01T00:00:00.000Z"
          },
          {
            "id": 4,
            "recordedAt": "2017-04-01T00:00:00.000Z"
          }
        ]
      },
      {
        "name": "South",
        "accounts": [
          {
            "id": 2,
            "recordedAt": "2012-05-01T00:00:00.000Z"
          }
        ]
      }
   ]
}

示例数据:transaction.jsonl

谢谢

0 个答案:

没有答案