使用 AQL 进行图形查询
Graph query using AQL
我有 3 个集合,第一个是列表(顶点),第二个是用户(顶点),第三个是 activity(边)。我计划将在列表中完成的任何类型的 activity 存储在 activity 集合中(例如喜欢、评论、标记等)。
假设我需要获取任何用户的所有列表以及他创建的每个列表上每个事件的计数,例如在单个查询中每个列表上的不喜欢、评论、标志。我怎么做?
我一直在探索 arangodb 文档大约 2 天,但无法弄清楚如何编写这样的查询。
如果每个个体 activity 都表示为两个顶点集合之间的一条边,那么找到一个用户,检索其邻居(列表顶点)并按类型(边属性?)对边进行分组:
FOR u IN users
FILTER u._key == "john"
FOR v, e IN 1..1 OUTBOUND u activities
// v is a list vertex document
// e is an activity edge document
COLLECT list = v._id, type = e.type, user = u._id
WITH COUNT INTO count
RETURN { list, user, type, count }
我的测试数据结果:
list | user | type | count
------------|------------|---------|------
lists/john1 | users/john | comment | 1
lists/john1 | users/john | modify | 3
lists/john1 | users/john | vote | 1
lists/john2 | users/john | delete | 1
lists/john2 | users/john | like | 1
lists/john2 | users/john | modify | 2
lists/john2 | users/john | vote | 2
如果你想保留列表和用户文档,你可以这样做:
FOR u IN users
FILTER u._key == "john"
FOR v, e IN 1..1 OUTBOUND u activities
COLLECT list = v._id, type = e.type
AGGREGATE count = LENGTH(1) INTO groups
RETURN {
list: groups[0].v,
user: groups[0].u,
type,
count
}
对于大量数据,以下可能会使用较少的内存,因为它避免跟踪组并改为查找必要的文档:
FOR u IN users
FILTER u._key == "john"
FOR v, e IN 1..1 OUTBOUND u activities
COLLECT list_id = v._id, user_id = u._id, type = e.type
WITH COUNT INTO count
RETURN {
list: DOCUMENT(list_id),
user: DOCUMENT(user_id),
type,
count
}
我有 3 个集合,第一个是列表(顶点),第二个是用户(顶点),第三个是 activity(边)。我计划将在列表中完成的任何类型的 activity 存储在 activity 集合中(例如喜欢、评论、标记等)。
假设我需要获取任何用户的所有列表以及他创建的每个列表上每个事件的计数,例如在单个查询中每个列表上的不喜欢、评论、标志。我怎么做?
我一直在探索 arangodb 文档大约 2 天,但无法弄清楚如何编写这样的查询。
如果每个个体 activity 都表示为两个顶点集合之间的一条边,那么找到一个用户,检索其邻居(列表顶点)并按类型(边属性?)对边进行分组:
FOR u IN users
FILTER u._key == "john"
FOR v, e IN 1..1 OUTBOUND u activities
// v is a list vertex document
// e is an activity edge document
COLLECT list = v._id, type = e.type, user = u._id
WITH COUNT INTO count
RETURN { list, user, type, count }
我的测试数据结果:
list | user | type | count
------------|------------|---------|------
lists/john1 | users/john | comment | 1
lists/john1 | users/john | modify | 3
lists/john1 | users/john | vote | 1
lists/john2 | users/john | delete | 1
lists/john2 | users/john | like | 1
lists/john2 | users/john | modify | 2
lists/john2 | users/john | vote | 2
如果你想保留列表和用户文档,你可以这样做:
FOR u IN users
FILTER u._key == "john"
FOR v, e IN 1..1 OUTBOUND u activities
COLLECT list = v._id, type = e.type
AGGREGATE count = LENGTH(1) INTO groups
RETURN {
list: groups[0].v,
user: groups[0].u,
type,
count
}
对于大量数据,以下可能会使用较少的内存,因为它避免跟踪组并改为查找必要的文档:
FOR u IN users
FILTER u._key == "john"
FOR v, e IN 1..1 OUTBOUND u activities
COLLECT list_id = v._id, user_id = u._id, type = e.type
WITH COUNT INTO count
RETURN {
list: DOCUMENT(list_id),
user: DOCUMENT(user_id),
type,
count
}