cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Newbie -Adding up a property in the relationship NEO4j

I am trying to add up properties in the relationship along a path. This is my first week with NEO4j and I am now stuck. If anyone can advise. Thank you

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

This path has a total of 4 nodes each with a different distance total of 19kms.

it gives an error of Type Mismatch expected Map but got list

Here is the code I did to generate the Graph

        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)
1 REPLY 1

I assumed you wanted to add total shortest distance between two given points.

You are getting error on sum(dist.distance) because sum is expecting list or map. So here is way to solve this.

sample code for generating graph.

merge  (c:Country {name: "South_Africa"}) 
            merge  (r:Region {name: "Durban"})   
            merge  (c1:City {name: 'Durban'})
            merge  (c3:City {name: 'cityA'})
            merge  (c4:City {name: 'CityB'})
            merge  (c5:City {name: 'CityC'})
            merge  (c2:City {name: 'Pinetown'})
            merge  (r)-[:IS_IN] -> (c)
            merge  (c1)-[:DISTANCE_TO {distance:toInteger(10)}] -> (c3)
            merge  (c3)-[:DISTANCE_TO {distance:toInteger(9)}] -> (c4)
            merge  (c4)-[:DISTANCE_TO {distance:toInteger(3)}] -> (c5)
            merge  (c5)-[:DISTANCE_TO {distance:toInteger(4)}] -> (c2)
            merge  (c1)-[:IS_IN] -> (r)
            merge  (c2)-[:IS_IN] -> (r)
            merge  (c3)-[:IS_IN] -> (r)
            merge  (c4)-[:IS_IN] -> (r)
            merge  (c5)-[:IS_IN] -> (r)

this will generate your graph.

Red Node is the city. I added an additional city node for clearly seeing the shortest distance between different cities.

Now the shortest distance between Durban and Pinetown city is.

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

It shows 26 km.

If you are interested only in finding path.

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