ArangoDB - 如何在图遍历中执行计算? [英] ArangoDB - how to perform computations in graph traversals?
问题描述
我有一个简单的图表来跟踪我借钱的人。
因此,图如下所示:
userB - 欠(金额:200) - > userA
userC - 欠(金额:150) - > userA
等等...
比方说,你需要找出每个用户欠多少钱,使用图遍历。你如何实现这一点?解析方案
让我使用城市示例图
顶点(城市)具有数字属性, population
;边缘(公路)有一个数值属性距离
。
检查我们期望的结果:
FOR v,e IN 1..1 INBOUNDfrenchCity / LyonGRAPHrouteplanner
RETURN {city:v,highway: e}
总结所有经过城市的人口是很容易的:
RETURN SUM(for v IN 1..1 INBOUNDfrenchCity / LyonGRAPHrouteplanner
RETURN v.population)
它使用一个子查询,这意味着返回所有值,然后 它更好地使用 SUM <
$ b COLLECT AGGREGATE
来总结遍历期间的属性。
因此,在城市人口数量和距离的背景下,将这些数字归纳起来可能没有意义,但无论如何,
FOR v,e IN 1..1 INBOUNDfrenchCity / LyonGRAPHrouteplanner
COLLECT AGGREGATE populationSum = SUM(v.population),distanceSum = SUM(e.distance)
RETURN {population:populationSum,距离:distanceSum}
I have a simple graph for keeping a track of people whom I have lent money to. So the graph looks like this:
userB -- owes to (amount: 200) --> userA
userC -- owes to (amount: 150) --> userA
and so on...
Lets say you need to find out how much money each user is owed, using a graph traversal. How do you implement this?
Let me explain this using the city example graph
Vertices (cities) have a numeric attribute, population
; Edges (highways) have a numeric attribute distance
.
Inspecting what we expect to sumarize:
FOR v, e IN 1..1 INBOUND "frenchCity/Lyon" GRAPH "routeplanner"
RETURN {city: v, highway: e}
Summing up the population of all traversed cities is easy:
RETURN SUM(FOR v IN 1..1 INBOUND "frenchCity/Lyon" GRAPH "routeplanner"
RETURN v.population)
This uses a sub-query, which means all values are returned, and then the SUM
operation is executed on them.
Its better to use COLLECT AGGREGATE
to sum up the attributes during the traversal.
So while in the context of population of cities and their distances it may not make sense to sumerize these numbers, lets do it anyways:
FOR v, e IN 1..1 INBOUND "frenchCity/Lyon" GRAPH "routeplanner"
COLLECT AGGREGATE populationSum = SUM(v.population), distanceSum = SUM(e.distance)
RETURN {population : populationSum, distances: distanceSum}
这篇关于ArangoDB - 如何在图遍历中执行计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!