Return multiple relationships as one in cypher

Consider a query where one can get from node A to node D, you have to go through a couple of relationships, for example.

(:Person)-[:LIVES_IN]->(:State)-[:HAS]->(:Parks)

      

and I wanted to return on the fly all the possible Parks to which a person can go to their state without returning the state itself in the query so that the returned ratio looks like

(:Person)-->(:Parks)

      

In other words, like this:

enter image description here

What does the request look like?

Thank you for your time!

+3


source to share


2 answers


You can use APOC to return a virtual relation something like this ...



MATCH (person:Person {name: 'Dave'})-[:LIVES_IN|HAS*]->(park:Park)
WITH person, park
CALL apoc.create.vRelationship(person,'CAN_VISIT',{}, park) YIELD rel
RETURN person, park, rel

      

+3


source


If you want to get all the Parks a person can access, you can simply request:



MATCH (:Person)-[*]->(p:Parks)
RETURN p

      

+1


source







All Articles