3
votes

I have a group of nodes which are related in the following way:

        A --> B --> C --> D

The relation is a "connected with" relationship with some id property. My question is: Can I write a cypher query which will show me all the id properties of each relationship from start node A to the start node D without mentioning in the query any of the nodes in between.

Thanks Dimitris

3

3 Answers

4
votes

Perhaps you are looking for a variable length relationship:

MATCH (a:A { name:"Some property" })-[r:connected_with*1..4]-(x)
RETURN r, x
2
votes

Consider the followng data

create (a:Test {name: "A"})
create (b:Test {name: "B"})
create (c:Test {name: "C"})
create (d:Test {name: "D"})
create a-[:CONNECTED_TO]->b
create b-[:CONNECTED_TO]->c
create c-[:CONNECTED_TO]->d
return *

The following query will return a collection of ids for each node in the path.

match p=(a:Test)-[:CONNECTED_TO*3]->(d:Test)
return reduce(nodes = [],n in nodes(p) | nodes + [id(n)]) as node_id_col
1
votes

Basing this off of Dave Bennett's data, the following cypher query would get the all the nodes and the relationships between then (provided they are all CONNECTED_TO) and their associated properties in a path of arbitrary length.

MATCH (x:Test)-[r:CONNECTED_TO*]->(z:Test)
RETURN x, r, z