ArangoDB - how to perform computations in graph traversals?

不想你离开。 提交于 2019-12-07 13:46:44

问题


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?


回答1:


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}


来源:https://stackoverflow.com/questions/35640507/arangodb-how-to-perform-computations-in-graph-traversals

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