cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to display nodes from left to right based on relationship direction

Hi. I'd like my browser results to appear with the nodes with no incoming relationships on the left, and then proceeding rightwards till the nodes with no outgoing relationships on the right.

Any way to do that?
Thanks
Nick

1 REPLY 1

Correct me if I'm wrong but it sounds to me that you're trying to query a variable-length path. Assuming that you have the following graph, where the green nodes represent (:Stop) nodes with [:ROUTE] as its edges or relationships:

The following query should work to first identify the start node with "no incoming relationships on the left", then traversing the graph through the [:ROUTE] relationships till the end of the chain.

MATCH (n:Stop), path = (n)-[:ROUTE*]->()
WHERE NOT exists((:Stop)-[:ROUTE]->(n))
RETURN path

If you've more than one relationship that you're wanting to traverse through, you can easily do that with

MATCH (n:Stop), path = (n)-[:ROUTE | EXIT*]->()
WHERE NOT exists(()-[*]->(n))
RETURN path

which should return something like this

Just be cautious to identify and limit your search scope by node labels and/or relationship types, or you might run the risk of returning a very large result set. This documentation might help you out.