Find all labels connected

Lets say node A->B->C->D->E->F and so on. Like node A is connected to B, B is connected to C, C is connected to D, D is connected to E and so on. Now is there way to get all connections when I query A.

Not sure exactly what you are asking for, but you can return the shortest path between A and F:

MATCH (a:A {prop: 'foo'}),
(f:F {prop: 'bar'}),
p = shortestPath((a)-[*]-(f))
RETURN nodes(p)
limit 50

Or less recommended:

MATCH p = (a:A)-[*5]->(f:F)
RETURN nodes(p)
limit 50

Both of these will return far more that you probably want if not filtered correctly.

I mean if the nodes are connected like this. I want to print from A to F as you said, but what if I don't know the last node. From my graph, I need to find all the mutual nodes connected to the node that I query.

Here when I query A, I need to get A,B,C,D,E,F. But i don't know my last node which is F here.

Hi Vence,

not very easy to understand the matter, and the context either.
I presume you want to find, starting from A and after 5 relations each "end node", which means nodes that do not have further relations (also called "leaf nodes"), and then all the nodes in-between.

// Find leaf nodes
MATCH p1=(A{uniqueId:"A"})-[r5]->(Last)  
optional match p2 = (Last)-[]->()
with p1,  count(p2) as cp where cp = 0
RETURN nodes(p1)

(I do not know if A,B,C... name or id of the node, of the label or of the variable of the node(s). The given cypher use A and Last as variable)
Hope it helps.

Hi @vence.andersen17 ,

You may like a look on:

Another more 'spartan' option on directed relationships is:

MATCH path = (a:A)-[:CON*]->()
UNWIND nodes(path) as n
return distinct n
1 Like