# Yet Another Repeat Until Question (YARUQ)

I want to return all nodes/relationships until I find the first node that has a certain relationship in an arbitrary number of iterations.

## Example

Given:

``````X(1)-[foo]->X(2)-[foo]->X(3)-[bar]->X(4)-[foo]->X(5)
``````

I want this returned:

``````X(1)-[foo]->X(2)-[foo]->X(3)-[bar]->X(4)
``````

This does NOT work:

``````MATCH (f:X), r=(f)-[:foo|bar*1..]->(t:X)
WHERE id(f) = 1 AND NOT ()-[:bar]->(f)
WITH f, relationships(r) AS rs, nodes(r) AS ns
RETURN f, rs, ns
``````

It returns the entire graph:

``````X(1)-[foo]->X(2)-[foo]->X(3)-[bar]->X(4)-[foo]->X(5)
``````

Is this because of the 2 WHERE conditions being temporally at odds?
How do I change it to work?
In addition, I'd like to filter on the ID of the last traversed relationship is in the terminating condition.
like (yes, this is invalid, silly syntax):

``````MATCH (f:X), r=(f)-[:foo|bar*1..]->(t:X)
WHERE id(f) = 1 AND NOT (f-1)-[:bar]->(f)
WITH f, relationships(r) AS rs, nodes(r) AS ns
RETURN f, rs, ns
``````

Is this all too complex for Cypher??

Figured it out the first question myself after all:

``````MATCH (f:X {Id:1}), x=(f)-[:foo|bar*1..]->(t:X)
WITH x, startNode(last(relationships(x))) AS s
WHERE NOT ()-[:bar]->(s)
RETURN x
``````

Someone can still answer whether (f-1) is possible somehow...
These 'functions' look promising..maybe if I can index into the list of x's rels...

``````MATCH (f:X {Id:1}), x=(f)-[:foo|bar*1..]->(t:X)