A quantified path is a variable-length path where the complete path or a part of it is repeated a specified number of times. Quantified paths are useful when you:
A quantifier is written as a postfix to either an edge pattern or a parenthesized path pattern to specify how many times the pattern should repeat.
| Quantifier | Description |
|---|---|
{m,n} | Between m and n repetitions. |
{m} | Exactly m repetitions. |
{m,} | m or more repetitions. |
{,n} | Between 0 and n repetitions. |
* | Between 0 and more repetitions. |
+ | Between 1 and more repetitions. |
A quantifier's upper bound must be at least 1. A quantifier that can only repeat zero times, such as {0}, {0,0}, or {,0}, is not allowed and raises an error.
A lower bound of 0 is allowed when paired with a positive upper bound ({0,n}, {,n}, or *): the pattern may match zero times. In the zero-repetition case, the quantified segment is skipped entirely, contributing none of its own nodes or edges, and the node patterns on either side of the quantifier are merged:
(a)(...){0,n}(b): a and b collapse onto the same node, combining their conditions with logical AND.(a)(...){0,n}: binds each a.(...){0,n}(b): binds each b.
GQLINSERT (jack:User {_id: "U01", name: "Jack"}), (mike:User {_id: "U02", name: "Mike"}), (c1:Device {_id: "Comp1"}), (c2:Device {_id: "Comp2"}), (c3:Device {_id: "Comp3"}), (c4:Device {_id: "Comp4"}), (jack)-[:Owns]->(c1), (mike)-[:Owns]->(c4), (c1)-[:Flows {packets: 20}]->(c2), (c1)-[:Flows {packets: 30}]->(c4), (c2)-[:Flows {packets: 34}]->(c3), (c2)-[:Flows {packets: 12}]->(c4), (c3)-[:Flows {packets: 74}]->(c4)
When writing a quantified path to its full form:
AND.Edge patterns can be directly followed by a quantifier, and both the full and abbreviated edge patterns are supported.

You can enclose the entire path pattern in parentheses () and append a quantifier.

You can enclose part of a path pattern in parentheses () and append a quantifier for it.

Another example:

GQLMATCH p = ({name: 'Jack'})->()-[f:Flows WHERE f.packets > 20]->{1,3}()<-({name: 'Mike'}) RETURN p
Result: p

GQLMATCH p = ((:Device)->(:Device)){2} RETURN p
Result: p

GQLMATCH ({_id: 'Comp1'})->{2,}(n) RETURN COLLECT_LIST(n._id)
Result:
| COLLECT_LIST(n._id) |
|---|
| ["Comp4","Comp3","Comp4"] |
GQLMATCH p = ({_id: 'Comp1'})-[f:Flows WHERE f.packets > 20]->*() RETURN p
Result: p

GQLMATCH p = ({_id: 'Comp1'})-[f:Flows WHERE f.packets > 20]->+() RETURN p
Result: p

GQLMATCH p = ({name: 'Jack'})->(()-[f:Flows WHERE f.packets > 20]->()){,2}<-({name: 'Mike'}) RETURN p
Result: p

Element variables declared within the repeatable part of a quantified path are bound to a list of nodes or edges, known as group variables or group list.

GQLINSERT (rowlock:User {_id: "U1", name: "rowlock", age: 24}), (quasar92:User {_id: "U2", name: "Quasar92", age: 29}), (claire:User {_id: "U3", name: "claire", age: 35}), (rowlock)-[:Follows {score: 2}]->(quasar92), (quasar92)-[:Follows {score: 3}]->(claire)
Whenever a group variable is referenced outside the repeatable part of the quantified path where it is declared, it refers to a list of nodes or edges.
In this query, variables a and b represent lists of nodes encountered along the matched paths, rather than individual nodes:
GQLMATCH p = ((a)-[]->(b)){1,2} RETURN p, a, b
Result:
| p | a | b |
|---|---|---|
![]() | [
{
"id": "U1",
"uuid": "662445893244688851",
"labels": [
"User"
],
"properties": {
"name": "rowlock",
"age": 24
}
}
] | [
{
"id": "U2",
"uuid": "662445893244688872",
"labels": [
"User"
],
"properties": {
"name": "Quasar92",
"age": 29
}
}
] |
![]() | [
{
"id": "U1",
"uuid": "662445893244688851",
"labels": [
"User"
],
"properties": {
"name": "rowlock",
"age": 24
}
},
{
"id": "U2",
"uuid": "662445893244688872",
"labels": [
"User"
],
"properties": {
"name": "Quasar92",
"age": 29
}
}
] | [
{
"id": "U2",
"uuid": "662445893244688872",
"labels": [
"User"
],
"properties": {
"name": "Quasar92",
"age": 29
}
},
{
"id": "U3",
"uuid": "662445893244688893",
"labels": [
"User"
],
"properties": {
"name": "claire",
"age": 35
}
}
] |
![]() | [
{
"id": "U2",
"uuid": "662445893244688872",
"labels": [
"User"
],
"properties": {
"name": "Quasar92",
"age": 29
}
}
] | [
{
"id": "U3",
"uuid": "662445893244688893",
"labels": [
"User"
],
"properties": {
"name": "claire",
"age": 35
}
}
] |
To aggregate the group variables, use the FOR statement to expand it into individual records:
GQLMATCH path = ()-[edges]->{1,2}() CALL (path, edges) { FOR edge IN edges RETURN sum(edge.score) AS scores } FILTER scores > 2 RETURN path, scores
Result:
| p | scores |
|---|---|
![]() | 3 |
![]() | 5 |
The following query throws syntax error since a and b are lists:
GQL - Syntax ErrorMATCH p = ((a)-[]->(b)){1,2} WHERE a.age < b.age RETURN p
A group variable has a singleton reference only when it is referenced within the repeatable part of the quantified path where it is declared.
In this query, a and b are treated as singletons that represent individual nodes. The condition a.age < b.age is evaluated for each pair of nodes a and b as the path is matched:
GQLMATCH p = ((a)-[]->(b) WHERE a.age < b.age){1,2} RETURN p
Result: p
