Operators are symbols or keywords used to perform operations on data.
Category | Operators |
|---|---|
| Property Accessor | . |
| Logical Operators | AND, OR, XOR, NOT |
| Arithmetic Operators | +, -, *, /, %, ^ |
| Assignment Operators | = |
| String Operators | String Concatenation: ||, +String Matching: CONTAINS, STARTS WITH, ENDS WITH |
| List Operators | List Construction: [] or LIST[] or ARRAY[]Elements Accessing: []Membership Checking: IN, NOT INList Concatenation: || |
| Path Operators | Path Construction: PATH[]Path Concatenation: || |
| Record Operators | Record Construction: {} or RECORD{}Field Reference: . |
| Deduplication | DISTINCT |
The . (dot) operator allows you to access a property of a graph element.
GQLMATCH (n) RETURN n._id LIMIT 10
Combines two or more conditions in a way that all of them must be true for the entire expression to evaluate to true.
Truth table for the AND operator:
AND | True | False |
|---|---|---|
| True | True | False |
| False | False | False |
This query returns users whose age exceeds 30 and incomeGroup equals to 4:
GQLMATCH (n:User) WHERE n.age > 30 AND n.incomeGroup = 4 RETURN n
Combines two or more conditions where only one of them needs to be true for the entire expression to evaluate to true.
Truth table for the OR operator:
OR | True | False |
|---|---|---|
| True | True | True |
| False | True | False |
This query returns users whose age exceeds 30, or incomeGroup equals to 4:
GQLMATCH (n:User) WHERE n.age > 30 OR n.incomeGroup = 4 RETURN n
Combines two or more conditions by evaluating two conditions at a time. For two conditions, the result is true only if exactly one of the conditions is true. If both are true or both are false, the result is false. When applied to multiple conditions, XOR first evaluates the result of the first two conditions, then compares that result with the next condition, continuing this process until all conditions are checked.
Truth table for the XOR operator:
XOR | True | False |
|---|---|---|
| True | False | True |
| False | True | False |
This query returns users whose age exceeds 30, or incomeGroup equals to 4, but excludes users who meet both criteria:
GQLMATCH (n:Person) WHERE n.age > 30 XOR n.incomeGroup = 4 RETURN n
Negates a condition, returning true if the specified condition is false and vice versa.
Truth table for the NOT operator:
NOT | True | False |
|---|---|---|
| False | True |
This query returns users whose age is not 30:
GQLMATCH (n:Person) WHERE NOT n.age = 30 RETURN n
Performs mathematical operations on numerical values. GQL supports the following arithmetic operators:
+-*/%^GQLRETURN 2 + 8
The = operator is used to assign values in statements like LET and SET, and to declare path variables within MATCH statements.
GQLLET a = 1 RETURN a
GQLMATCH (n:Person WHERE n.name = "John Doe") SET n.gender = "male"
GQLMATCH p = ()->() RETURN p
The || or + operators combines multiple strings into a single string by merging the characters of each string in order.
GQLRETURN "data" || "base", // "database" "data" + "base" // "database"
The CONTAINS operator checks if one string contains another (case-sensitive).
This query returns user nodes whose aboutMe contains "graph database":
GQLMATCH (n:user WHERE n.aboutMe CONTAINS "graph database") RETURN n
You can transform all letters to upper or lower cases for case-insensitive matching:
GQLMATCH (n:user WHERE lower(n.aboutMe) CONTAINS "graph database") RETURN n
The CONTAINS operator is also used to match the specified keywords with tokens of a full-text index (precise or fuzzy match). See Full-text Index.
This query finds nodes using the full-text index prodDesc where their tokens include "graph" and "database":
GQLMATCH (n WHERE ~prodDesc CONTAINS "graph database") RETURN n
The STARTS WITH operator checks if a string starts with a given prefix (case-sensitive):
GQLMATCH (n:Paper WHERE n.title STARTS WITH "Efficient") RETURN n
The ENDS WITH operator checks if a string ends with a given suffix (case-sensitive):
GQLMATCH (n:Paper WHERE n.title ENDS WITH "Search") RETURN n
The [] or LIST[] or ARRAY[] can create a list by placing comma-separated elements inside.
GQLLET items = [1,2,3] RETURN items // [1, 2, 3]
The [] can also construct a nested list:
GQLLET items = [[1,2],[2,3]] RETURN items // [[1,2],[2,3]]
The [] can access elements within a list by their indexes. Lists use 0-based indexing, meaning the first element is at index 0.
Format | Elements Accessed |
|---|---|
[m] | The element with the index of m. |
[-m] | The m-th element to the bottom. |
[m:] | From the element with the index of m to the last element. |
[:n] | From first element to the element with the index of n-1. |
[m:n] | From the element with the index of m to the element with the index of n-1. |
Note: m and n are both positive integers and n > m.
GQLLET items = ["a", 1, "b", 34] RETURN items[0], items[1], items[-1], items[-2], items[1:], items[:2], items[1:3]
Result:
| items[0] | items[1] | items[-1] | items[-2] | items[1:] | items[:2] | items[1:3] |
|---|---|---|---|---|---|---|
| a | 1 | 34 | b | [1, "b", 34] | ["a", 1] | [1, "b"] |
The IN checks whether a specified element exists within a list. NOT IN checks that it does not. Both evaluate to 1 for true and 0 for false.
GQLMATCH (n) WHERE n._id IN ["U01", "U02"] RETURN n
GQLMATCH (n) WHERE n._id NOT IN ["U01", "U02"] RETURN n
The concatenation operator || combines multiple lists into a single list by merging the elements of each list in order.
GQLRETURN [1,2,3] || [3,4,5] // [1, 2, 3, 3, 4, 5]
The PATH[] creates a path by enumerating node and edge references in order.
GQLMATCH (a {_id: "P1"})-[e]->(b) RETURN PATH[a, e, b]
The concatenation operator || joins multiple paths into a continuous single path, merging the last node of the first path with the first node of the second path when they are identical.
GQLMATCH p1 = ({_id: "P1"})->(n), p2 = (n)->() RETURN p1 || p2
The {} or RECORD{} creates a record. You can define its fields and corresponding values.
GQLLET rec = {length: 20, width: 59, height: 10} RETURN rec.length // 20
The . (period) operator allows you to reference a field of a record.
GQLLET rec = RECORD{length: 20, width: 59, height: 10} RETURN rec.length * rec.width * rec.height // 11800
The DISTINCT ensures that only unique values are included.
This query returns distinct age values of users:
GQLMATCH (n:User) RETURN DISTINCT n.age