简体   繁体   中英

how to find the neighbors of a node in neo4j?

I have this nodes in my neo4j. It is about a Family tree.

Here is the picture: Graph View

I want to find the all the neighbors for a given node from the leaf. Is that possible? can anyone help me?

explanation:

This Cypher query returns all the grandchildren for 'Lucas Hankinson':

MATCH(n:FamilyTree{name: 'Lucas Hankinson'})-[*2..2]->(m) RETURN collect(m)

If I was given the name of one of Lucas's grandchildren and I want to see all his/her cousins and siblings(basically the neighbor nodes of that grandchild) how can I do that in a Cypher Query.

You can try using the path notion. eg From the given Grand Child name (eg Elwood Alger) find the Grand Parent using path (in this case Lucas Hankinson) and then find all the Grand Children.

Query:

MATCH (gc:FamilyTree{name: 'Elwood Alger'})<--(p:FamilyTree)<--(gp:FamilyTree)
OPTIONAL MATCH (gp) -[*2..2]->(m) WHERE m.name <> 'Elwood Alger' RETURN collect(m)

Thanks, Vishal

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