I want to update attribute `regions` by the following conditions:
- If region R does not exist in `regions` then push new region R to
`regions`
- Else if region R exists then update attribute `accounts` for that
region R in `regions`
So that no duplicate region for the same edge and no duplicate account for
the same region.
This is my 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
Actual Result:
{
"_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"
}
]
}
]
}
Expected Result:
{
"_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"
}
]
}
]
}
Actual result has duplicate North region.
Thank you
--
You received this message because you are subscribed to the Google Groups
"ArangoDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
transaction.json
Description: application/json
