Search code examples
neo4jcypher

Cypher - given relationship, get the nodes


If I do the query

MATCH (:Label1 {prop1: "start node"}) -[relationships*1..10]-> ()
UNWIND relationships as relationship
RETURN DISTINCT relationship

How do I get nodes for each of acquired relationship to get result in format:

╒════════╤════════╤═══════╕
│"from"  │"type"  │"to"   │
╞════════╪════════╪═══════╡
├────────┼────────┼───────┤
└────────┴────────┴───────┘

Is there a function such as type(r) but for getting nodes from relationship?


Solution

  • Yes, such functions do exist!

    • startNode(r) to get the start node from relationship r
    • endNode(r) to get the end node

    Here's the final query:

    MATCH () -[relationships*1..10]-> ()
    UNWIND relationships as r
    RETURN startNode(r) as from, type(r) as type, endNode(r) as to