These examples use the sample graph from the matching page.
Variable-length paths
TuringDB supports both its own postfix quantifiers and the Neo4j bracket form:
| Syntax | Hops |
|---|---|
-[:KNOWS]->+ | 1 or more |
-[:KNOWS]->* | 0 or more (includes the start node itself) |
-[:KNOWS]->{2,4}, {2}, {,3}, {2,} | between, exactly, up to, at least |
-[:KNOWS*1..3]->, -[:KNOWS*2]->, -[*..3]-, -[*0..1]-> | Neo4j style |
-[:KNOWS*]-> | 1 or more (unlike the postfix *) |
MATCH (a:Person {name: 'Alice'})-[:KNOWS]->+(b)
RETURN DISTINCT b.name
MATCH (a:Person {name: 'Alice'})-[:KNOWS*1..3]->(b)
RETURN DISTINCT b.name
MATCH (a)-[:KNOWS|WORKS_AT]->{1,2}(b)
RETURN a.name, b.name
MATCH (a)-[e:KNOWS WHERE e.since > 2012]->+(b)
RETURN a.name, b.name
MATCH (a)-[:KNOWS*1..3 {since: 2010}]->(b)
RETURN b.name
MATCH (a {name: 'Alice'})-[e:KNOWS]->+(b)
RETURN b.name, size(e)
MATCH (a {name: 'Alice'})-[e:KNOWS]->+(b)
RETURN b.name, [r IN e | r.since] AS sinces- Edge type alternatives (
[:KNOWS|WORKS_AT]) work on variable-length hops, and so does either direction written with both arrow heads:(a)<-[:KNOWS]->{1,2}(b). - A predicate or property map applies to every hop.
-[e:KNOWS WHERE e.since > 2012]->+only follows edges that satisfy it, and-[:KNOWS*1..3 {since: 2010}]->requires every edge on the path to have that property value. - Trail semantics. Each edge is used at most once within a path, while nodes may repeat. Each distinct path is its own row, so use
DISTINCTor an aggregate to collapse them. - One quantifier per edge. Choose either
[*1..3]or{1,3}for an edge. - The edge variable of a variable-length hop is a list of edges.
size(e),e[0],e[-1],UNWIND eand comprehensions such as[r IN e | r.since]work on it.
Variable-length matching enumerates paths. On dense graphs the number of paths can grow very quickly, so always bound the length (*1..6 or {1,6}). For weighted shortest paths on large graphs, use the shortestPath statement.
Quantified path patterns
A quantified path pattern repeats a sub-pattern, with its own WHERE, a number of times:
MATCH (a:Person {name: 'Alice'})
((x)-[r:KNOWS]->(y) WHERE r.since > 2009){1,3} (b)
RETURN b.name
MATCH (a:Person {name: 'Alice'})
((x)-[:KNOWS]->(y) WHERE y.age > x.age)+ (b)
RETURN b.name
MATCH (a:Person {name: 'Alice'})
((x)-[:KNOWS]->(y)-[:KNOWS]->(z)){1,2} (b)
RETURN b.name
MATCH (a:Person {name: 'Alice'})
((x)-[r:KNOWS]->(y)){1,3} (b)
RETURN b.name, [n IN y | n.name] AS via, size(r) AS hopsThe second query follows KNOWS edges only while ages strictly increase, and the third repeats a two-hop sub-pattern. The inner variables (x, y, r) are group variables: each becomes a list with one element per repetition, which you can use in comprehensions, size(), UNWIND and sort keys:
MATCH (a:Person {name: 'Alice'})
((x)-[r:KNOWS]->(y)){1,3} (b:Person {name: 'Dave'})
UNWIND r AS e
RETURN e.since
MATCH (a:Person {name: 'Alice'})
((x)-[r:KNOWS]->(y)){1,3} (b)
RETURN b.name
ORDER BY size(r) DESC LIMIT 1The WHERE inside the parentheses can test the edge and both nodes of each repetition: ((x)-[r:KNOWS]->(y) WHERE r.since >= 2010 AND y.age > 25){2}.
Named paths
Give a path a name to inspect its nodes and edges:
MATCH p = (a:Person {name: 'Alice'})-[:KNOWS]->+(b)
RETURN [n IN nodes(p) | n.name] AS names,
[r IN relationships(p) | r.since] AS sinces,
length(p) AS hops
ORDER BY hopsnodes(p)andrelationships(p)return lists of IDs. Read properties through a list comprehension.length(p)is the number of hops.- A bare
RETURN pcomes back as a list of IDs, node and edge alternating:[0, 0, 1]is node 0, edge 0 and node 1. count(p)counts paths, andRETURN DISTINCT pgroups on whole paths.OPTIONAL MATCH p = ... RETURN p IS NULLworks.
Paths are values like any other: collect(p) gathers them, lists and maps hold them, and a pattern comprehension can name its path:
MATCH p = (a:Person {name: 'Alice'})-[:KNOWS]->+(b)
WITH collect(p) AS routes
RETURN size(routes) AS routeCount,
reduce(t = 0, q IN routes | t + length(q)) AS totalHops
MATCH (a:Person {name: 'Alice'})
RETURN [p = (a)-[:KNOWS]->+() | length(p)] AS lengths
MATCH p = (a:Person {name: 'Alice'})-[:KNOWS]->(b)
RETURN {route: p, hops: length(p)} AS summaryA property cannot hold a path: A property value cannot hold a path, in a list or a map or on its own. Store the IDs or the properties you need instead.
Filtering paths with allReduce
allReduce(acc = init, x IN list | step, predicate) folds a list like reduce and is true when the predicate holds for the accumulator after every step. Over the nodes or edges of a path, it tests a running total at each hop:
MATCH p = (a:Person {name: 'Alice'})-[:KNOWS]->+(b)
WHERE allReduce(
total = 0, n IN nodes(p) | total + n.age,
total <= 100
)
RETURN b.name, length(p) AS hopsThe ages along Alice → Bob → Carol add up to 93, so Bob and Carol are kept. Adding Dave makes 134, so the path to Dave is dropped.
A map accumulator carries more than one value from step to step. This query keeps the paths along which the since years of the edges increase, by comparing each edge with the one before:
MATCH p = (a:Person {name: 'Alice'})-[:KNOWS]->+(b)
WHERE allReduce(
acc = {}, r IN relationships(p) |
{prev: acc.cur, cur: r.since},
acc.prev IS NULL OR acc.prev < acc.cur
)
RETURN b.name, length(p) AS hopsThe initial value is never tested, and an empty list gives true. See Lists for the details.
Finding routes
For hop-count questions, undirected search, or when you need the properties along a route, combine variable-length paths with comprehensions:
// Fewest hops, returning the names along the route
MATCH p = (a:Person {name: 'Alice'})
-[:KNOWS*1..6]->(b:Person {name: 'Dave'})
RETURN [n IN nodes(p) | n.name] AS route, length(p) AS hops
ORDER BY hops LIMIT 1
// k-hop neighbourhood and reachability
MATCH (a:Person {name: 'Alice'})-[:KNOWS]-{1,3}(b)
RETURN DISTINCT b.name
MATCH (a:Person {name: 'Alice'})-[:KNOWS]->+(b)
RETURN count(DISTINCT b)To sum a property along a route, fold the path’s edges with reduce:
MATCH p = (a:Person {name: 'Alice'})
-[:KNOWS*1..6]->(b:Person {name: 'Dave'})
RETURN [n IN nodes(p) | n.name] AS route,
reduce(t = 0, r IN relationships(p) | t + r.since) AS total
ORDER BY total LIMIT 1To aggregate over the edges of every matched path at once, UNWIND relationships(p) AS r and aggregate r.since.
For a true weighted shortest path, see Shortest path.

