python - 如何在 arangodb 上存储聚合
问题描述
想象一下我有一个这样的边缘文档:
[{
"_from": "mobiles/12345",
"_to": "mobiles/54321",
"type": "call",
},
{
"_from": "mobiles/54321",
"_to": "mobiles/32145",
"type": "sms",
},
{
"_from": "mobiles/54321",
"_to": "mobiles/12345",
"type": "call",
}]
我需要在 54321 上查询时得到这样的列表:
{"54321":3, "12345":2,"32145":1}
我试过这个,但这不是我要找的:
for v,e,p in any "mobiles/54321" docs
COLLECT from = e._from , to = e._to with count into len
return {from, to, len}
我在 Elasticsearch 中使用 aggs 查询很容易做到这一点
解决方案
您可以“展开” _from 和 _to 属性,然后按文档键的联合而不是每个唯一组合进行分组,计算每个键出现的频率,并使用动态属性键为每个存储桶返回一个对象。外部 MERGE() 创建将键映射到计数的最终对象:
RETURN MERGE(
FOR v,e IN ANY "mobiles/54321" docs
FOR id IN [e._from, e._to]
COLLECT key = PARSE_IDENTIFIER(id).key WITH COUNT INTO len
RETURN { [key]: len }
)
推荐阅读
- android - 查找上一个活动片段
- python - 在excel中日期到文本,反之亦然
- c# - 在 OpenGL C# 中放大/缩小
- python - 如何使用玩家的分数和姓名制作高分列表
- php - 从 PHP 类中的数据库中获取数据
- javascript - Vue应用服务器和Express如何结合?
- regex - 匹配 url(" ") 内的路径字符串但不匹配 base64 的正则表达式
- django - 如何访问 OneToOneFiled Django ORM 模型的数据
- reactjs - 像ticketmaster一样的交互式平面图
- azure - 用于配置 IoT 中心设备 Azure 的经济高效的方法