在关系NEO4j中加一个属性

Adding up a property in the relationship NEO4j

我正在尝试沿路径添加关系中的属性。这是我使用 NEO4j 的第一周,现在我被卡住了。如果有人可以建议。谢谢

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
return path, sum(dist.distance)

此路径共有4个节点,每个节点的距离总计19公里。

它给出了 Type Mismatch expected Map but got list

的错误

这是我生成图表的代码

        merge  (c:Country {name: $Country}) 
            merge  (r:Region {name: $Region})   
            merge  (c1:City {name: $cityfrom})
            merge  (c2:City {name: $cityto})
            merge  (r)-[:IS_IN] -> (c)
            merge  (c1)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c2
            merge  (c2)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c1)
            merge  (c1)-[:IS_IN] -> (r)
            merge  (c2)-[:IS_IN] -> (r)

toInteger是你的朋友。

            merge  (r:Region {name: $Region})   
            merge  (c1:City {name: $cityfrom})
            merge  (c2:City {name: $cityto})
            merge  (r)-[:IS_IN] -> (c)
            merge  (c1)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c2)
            merge  (c2)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c1)
            merge  (c1)-[:IS_IN] -> (r)
            merge  (c2)-[:IS_IN] -> (r)

给定一个 path 变量,您可以使用 relationships(path) 获取它的关系列表。

您不能在路径或关系列表上使用 sum(),因为 sum() 是应用于多行的聚合函数,而不是应用于列表的缩放函数。

你有一些选择。

如果将关系列表展开为行,则可以使用 sum():

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
UNWIND relationships(path) as rel
return path, sum(rel.distance) as sum

您可以安装APOC程序并使用apoc.coll.sum()对集合的元素求和:

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
return path, apoc.coll.sum([rel IN relationships(path) | rel.distance]) as sum

您可以使用 reduce() 函数对距离求和:

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
return path, reduce(sum=0, rel IN relationships(path) | sum + rel.distance) as sum