Sofus Albertsen
Sofus Albertsen

Reputation: 193

Find all relations starting with a given node

In a graph where the following nodes

A,B,C,D

have a relationship with each nodes successor

(A->B)

and

(B->C)

etc.

How do i make a query that starts with A and gives me all nodes (and relationships) from that and outwards. I do not know the end node (C).

All i know is to start from A, and traverse the whole connected graph (with conditions on relationship and node type)

Upvotes: 0

Views: 1183

Answers (2)

Tim Kuehn
Tim Kuehn

Reputation: 3251

To get all the nodes / relationships starting at a node:

MATCH (a:A {id: "id"})-[r*]-(b)
RETURN a, r, b

This will return all the graphs originating with node A / Label A where id = "id".

One caveat - if this graph is large the query will take a long time to run.

Upvotes: 0

Brakebein
Brakebein

Reputation: 2237

I think, you need to use this pattern: (n)-[*]->(m) - variable length path of any number of relationships from n to m. (see Refcard)

A sample query would be:

MATCH path = (a:A)-[*]->()
RETURN path

Have also a look at the path functions in the refcard to expand your cypher query (I don't know what exact conditions you'll need to apply).

Upvotes: 1

Related Questions