如何使用AQL将数据分组到ArangoDB中?例如,我的结构是:
[
{name: "karl", id: "1", timestamp: "11112"},
{name: "migele", id": "2", timestamp: "11114"},
{name: "martina", id": "2", timestamp: "11116"},
{name: "olivia", id": "3", timestamp: "11118"},
{name: "sasha", id": "4", timestamp: "111120"},
]
我希望收到具有唯一ID和实际时间戳的数据:
{
karl,
martina (because martina timestamp > migele timestamp and his ids is equals),
olivia,
sasha
}
答案 0 :(得分:5)
要进行分组,您可以使用COLLECT
:
FOR doc IN collection
COLLECT id = doc.id INTO g
RETURN { id: id, docs: LENGTH(g) }
这将提供一个包含唯一ID的列表,对于每个唯一ID,您将收到带有id的文档数。
现在,要获取timestamp
中值最高的每个组中的文档,首先需要按时间戳对每个组进行排序:
FOR doc IN collection
COLLECT id = doc.id INTO g
LET names = (FOR value IN g[*].doc SORT value.timestamp DESC RETURN value.name)
RETURN names
最后,要仅接收时间戳值最高的成员,请使用names[0]
(您之前也可以应用LIMIT
,因为您只对第一项感兴趣):
FOR doc IN collection
COLLECT id = doc.id INTO g
LET names = (FOR value IN g[*].doc SORT value.timestamp LIMIT 1 DESC RETURN value.name)
RETURN names[0]