ArangoDB - how to perform computations in graph traversals?

非 Y 不嫁゛ 提交于 2019-12-05 23:13:59

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}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!