Search code examples
neo4jcypher

How to get Neo4j nodes linked through another node?


I have node A and an associated node B. Node C and node D are tied to node B too. It is necessary to get all nodes A and associated with them C and D

MATCH (a:A) -- (:B) -- (c:C) 
RETURN a, c

Received A and bound to it (via B) C. Is it possible get node A, C and D in the same request?


Solution

  • You can write your query in two ways like below:

    Option #1:

    MATCH (a:A) -- (b:B) -- (c:C), (b) -- (d:D)
    RETURN a, c, d
    

    Option #2:

    MATCH (a:A) -- (b:B) -- (c:C)
    MATCH (b) -- (d:D)
    RETURN a, c, d
    

    which is basically the same thing with the first query.