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 count number of hops in a query?

pborah88
Node Clone

Say I have a graph (object0)-[:CONNECTS_TO]->(object1)-[:CONNECTS_TO]->(Object2)-.....

I want a query that returns-

ColumnA       ColumnB            NumberOfHopsAway
***********  **********        ***********************
Object0        Object 1                     1
Object0        Object 2                     2
Object0        Object 3                      3

Thank you.

1 REPLY 1

Making some assumptions about your graph data (labels and properties), but you would use variable-length patterns in your MATCH something like this:

MATCH path = (start:Object {name:'Object0'})-[:CONNECTS_TO*]->(object:Object)
RETURN start.name as ColumnA, start.name as ColumnB, length(path) as NumberOfHopsAway