Neo4j Cypher - termination conditions when using variable level paths

I have a linked list that is modeled in a circular fashion, for example:

(u:User)
-[:LINK]->(a:NODELINK {linkId: 'aa'})
-[:LINK]->(b:NODELINK {linkId: 'bb'})
-[:LINK]->(c:NODELINK {linkId: 'cc'})
-[:LINK]->(d:NODELINK {linkId: 'dd'})
-[:LINK]->(u)

When I request it starting with node (b:NODELINK {linkId: 'bb'}), I would like to combine all the nodes until I reach the end / beginning of the list. (User node)

When I run the following query:

MATCH (u:USER)
WITH u
MATCH (nl:NODELINK)-[:LINK*]->(m)
WHERE nl.linkId = 'bb' AND m <> u
RETURN m

He returns me the following nodes

(3:NODELINK {linkId:'cc'})
(4:NODELINK {linkId:'dd'})
(1:NODELINK {linkId:'aa'})
(2:NODELINK {linkId:'bb'})

As you can see, my query wraps around and starts returning nodes from the very beginning of the list. I would like to end the node user and only return the nodes to the end of the list

(3:NODELINK {linkId:'cc'})
(4:NODELINK {linkId:'dd'})

I created a graph that demonstrates the problem here

How do I request to start with a node of interest to the link and return all the nodes before it reaches the node user?

0
2

WHERE node , , , , node.

, , "cc" node node ( , t node).

MATCH path=(nl:NODELINK {linkId:'cc'})-[:LINK*]->(u:USER)
RETURN nodes(path)

RETURN nodes(path)[..-1]

, .

0

. , NODELINK, MATCH, , , user node user.

MATCH (nl:NODELINK)
WHERE nl.linkId = 'aa'
WITH nl
MATCH p=(nl)-[:LINK*1..]->(m:USER)
RETURN filter(t IN nodes(p) 
              WHERE t <> nl) AS nodes
0

Source: https://habr.com/ru/post/1530123/


All Articles