我想将3个集合的结果合并为单个集合,但我无法合并最终结果的finishedStatus属性。
最终结果集合:
鉴于
收藏A
{"status": ["1","2","3"], "machine": "A", "finishedStatus": [{"status": "4", "count": 10}, {"status": "5", "count": 1}]}
{"status": ["4","5","6"], "machine": "A", "finishedStatus": [{"status": "3", "count": 5}, {"status": "5", "count": 11}]}
收藏B
{"status": ["1","2","3"], "machine": "B", "finishedStatus": [{"status": "1", "count": 3}, {"status": "5", "count": 14}]}
{"status": ["2","5","2"], "machine": "B", "finishedStatus": [{"status": "5", "count": 5}, {"status": "3", "count": 5}]}
收藏C
{"status": ["2","5","2"], "machine": "C", "finishedStatus": [{"status": "5", "count": 2}, {"status": "3", "count": 5}]}
{"status": ["3","2","1"], "machine": "C", "finishedStatus": [{"status": "2", "count": 6}, {"status": "4", "count": 7}]}
收集结果
{"status": ["1","2","3"], "machine": ["A", "B"] , "finishedStatus": [{"status": "1", "count": 3}, {"status": "4", "count": 10}, {"status": "5", "count": 15}]}
{"status": ["2","5","2"], "machine": ["B", "C"], "finishedStatus": [{"status": "5", "count": 7}, {"status": "3", "count": 10}]}
{"status": ["3","2","1"], "machine": ["C"], "finishedStatus": [{"status": "2", "count": 6}, {"status": "4", "count": 7}]}
{"status": ["4","5","6"], "machine": ["A"], "finishedStatus": [{"status": "3", "count": 5}, {"status": "5", "count": 11}]}
如何编写AQL INSERT / UPDATE / UPSERT以获得最终结果?
这是我的AQL
FOR doc IN A
UPSERT {"status": doc.status}
INSERT {"status": doc.status, "machine": [doc.machine], "finishedStatus": doc.finishedStatus}
UPDATE {
"machine": APPEND(OLD.machine, doc.machine, true),
"finishedStatus": <-- I cannot write AQL to update finishedStatus
How to write AQL to update this property?
}
IN result
谢谢
答案 0 :(得分:2)
这是AQL支持的内联if / else格式:
FOR doc IN A
UPSERT {"status": doc.status}
INSERT {"status": doc.status, "machine": [doc.machine], "finishedStatus": doc.finishedStatus}
UPDATE {
"machine": APPEND(OLD.machine, doc.machine, true),
"finishedStatus": (
OLD.finishedStatus[i].status == doc.finishedStatus[i].status ?
SUM([OLD.finishedStatus[i].count, doc.finishedStatus[i].count]) :
APPEND(OLD.finishedStatus, doc.finishedStatus[i])
)
}
IN result
我无法在没有您的数据集的情况下对此进行测试,但请注意内联if-then-else格式。
( comparator ? is_true : is_false )
(a == 1 ? 'one' : 'not one')
AQL将评估if-then-else,然后将其替换为&#39; true&#39;或者&#39; false&#39;你提供的价值。