cancel
Showing results for 
Search instead for 
Did you mean: 

How to write a query to update relationship between nodes of a path of n hops

vignes_k1
Node Clone

I have a set of nodes to track trip for trucks with GPS location sequence like
(t:Trip)-[:AT_LOCATION]-(g:GPSLocation)-[:NEXT*]->(g2:GPSLocation)
g and g2 gives me list of nodes.

I want to update the relationship between each subsequent nodes with the distance and time between each of the GPSLocation node. The data is present as a point() and datetime() in each of the GPS location nodes.

I am having difficulty to find a way to get the node(0) and node(1) and update the NEXT relationship with property for distance and time between each of the nodes.

I have tried to COLLECT() but do not know how to access the 1st, and 2nd, 2nd and 3rd and so on nodes.
Sorry if this sounds really trivial but it just escapes me.

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @vignes_k1

You can simplify the problem since you want to update all relationships (I don't know the name of your properties):

MATCH (a:GPSLocation)-[r:NEXT]->(b:GPSLocation)
SET r.distance = distance(a.point, b.point), r.duration = a.date - b.date

If you have a lot to update, you can use: apoc.periodic.iterate() function.

Regards,
Cobra

View solution in original post

1 REPLY 1

Cobra
Ninja
Ninja

Hello @vignes_k1

You can simplify the problem since you want to update all relationships (I don't know the name of your properties):

MATCH (a:GPSLocation)-[r:NEXT]->(b:GPSLocation)
SET r.distance = distance(a.point, b.point), r.duration = a.date - b.date

If you have a lot to update, you can use: apoc.periodic.iterate() function.

Regards,
Cobra

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.