Pavel Vanchugov
Pavel Vanchugov

Reputation: 393

Find last node in unknown amount of relationships

I can find last node like this

MATCH p=(a)-->(b)-->(c)
WHERE a.name='Object' AND c:Prime
RETURN c

But how i would find last node if i don't know how many relationships -->()-->() between two nodes?

I am trying to find last Node name by the Lable name. Last node doesn't have any outgoing relationships.

Upvotes: 2

Views: 277

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

This will find c in an arbitrarily long path where c has not outgoing relationships.

MATCH p=(a)-[*]->(c:Prime)
WHERE a.name='Object' 
AND not( c-->() )
RETURN c

It is generally advisable to use relationship types (if possible / practical) in your query and put an upward boundary on the number of hops your match can make. The example below follows only relationships of type CONNECTION in one direction to a maximum of 5 relationships.

MATCH p=(a)-[:CONNECTION*..5]->(c:Prime)
WHERE a.name='Object' 
AND not( c-->() )
RETURN c

Upvotes: 3

Related Questions