简体   繁体   中英

Neo4j cypher: find node at detph 2, without circular path

I would like to find all nodes at distance 2 from the current node:

eg:
1<->2
2<->3
1->3
2->4

A of this kind from node 1, should find node 4

I've tried this query, but it is suffering of circular paths:

start n=node({startid})
match n--> m
with distinct m as f1
match f1-->m
with distinct m as f2
return count(f2)

in fact, it finds also 1,2,3,4 as node at distance 2, without considering that 1 should be at distance 0, 2,3 distance 1, and only 4 is at distance 2.

any advice?

Do you mean something like this:

START n=node({startid})
MATCH (n)-[*..2]->m
RETURN m

After the * you can define the length of the path. *..2 means: Length between zero and 2.

START n=node({startid})
MATCH (n)-[*2]->m
WHERE n <> m
RETURN m

For fix length 2 and the WHERE will make sure n is not returned.

For the full documentation: http://docs.neo4j.org/chunked/milestone/query-match.html#match-variable-length-relationships

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM