TuringDB home pagelight logodark logo
  • Discord
  • GitHub
  • Website
TuringDB

Get Started

  • Introduction
  • Get Started
  • Claude Code Skill
  • Commands

Concepts

  • Overview
  • A Columnar Graph Database
  • Versioning System
  • The DataPart System
  • Zero-Locking Architecture
  • Snapshots Isolation

Benchmarks

  • Results Summary
  • Technical Report

Query Language

  • Overview
  • Matching
  • Paths
  • Results and Aggregation
  • NEXT and WHEN
  • Subqueries
  • Functions and Types
  • Writing Data
  • Procedures and Commands
  • CheatSheet

Importing data

  • JSONL
  • CSV
  • Neo4j
  • GML
  • Parquet

Graph Development

  • Create Graph
  • Load Graph
  • Load External data
  • Create a Change
  • List Changes
  • Create Nodes and Edges
  • Update Properties
  • Submit a Change
  • Time Travel
  • Graph Examples

Vector Search

  • Vector Search

Graph Algorithms

  • Shortest Path

Tutorials

  • Example Notebooks

Python SDK

  • Get Started
  • Reference

JavaScript SDK

  • Get Started
  • Reference

Release Notes

  • All Releases
  • TuringDB 3.1

Troubleshooting

  • Troubleshooting

  • MATCH
  • Label expressions
  • Filtering inside the pattern
  • Ordering and limiting in MATCH
  • OPTIONAL MATCH
  • WHERE and operators
  • Comparing numbers
  • Joins and Cartesian products
  • Look up nodes by native node ID
Query Language

Matching Patterns

Read data with MATCH, OPTIONAL MATCH and WHERE, combine labels with label expressions, join patterns, and look up nodes quickly by native node ID.

Read queries run directly against the current graph, with no change to open first.

The examples on this page use a small graph of people, companies and interests. Create it once in a change (see Writing data) if you want to follow along:

CREATE
  (alice:Person {name: 'Alice', age: 30, city: 'London',
    active: true, tags: ['admin', 'vip']}),
  (bob:Person {name: 'Bob', age: 35, city: 'London',
    active: true, tags: ['dev']}),
  (carol:Person {name: 'Carol', age: 28, city: 'Paris',
    active: false, tags: ['dev', 'vip']}),
  (dave:Person {name: 'Dave', age: 41, city: 'Paris',
    active: true}),
  (acme:Company {name: 'Acme', founded: 1999}),
  (globex:Company {name: 'Globex', founded: 2010}),
  (music:Interest {name: 'Music'}),
  (alice)-[:KNOWS {since: 2010}]->(bob),
  (bob)-[:KNOWS {since: 2015}]->(carol),
  (carol)-[:KNOWS {since: 2018}]->(dave),
  (alice)-[:WORKS_AT {role: 'CTO'}]->(acme),
  (bob)-[:WORKS_AT {role: 'Engineer'}]->(acme),
  (carol)-[:WORKS_AT {role: 'Engineer'}]->(globex),
  (alice)-[:LIKES]->(music),
  (carol)-[:LIKES]->(music)

​
MATCH

MATCH (n)
RETURN count(n)

MATCH (n:Person)
RETURN n.name, n.age

MATCH (n:Person {name: 'Alice', age: 30})
RETURN n.name

MATCH (n:Person:Employee)
RETURN n.name

MATCH (a:Person)-[:KNOWS]->(b:Person)
RETURN a.name, b.name

MATCH (a)-[e:KNOWS {since: 2010}]->(b)
RETURN a.name, e.since, b.name

MATCH (a)<-[e]-(b)
RETURN a.name, b.name

MATCH (a)-[e]-(b)
RETURN a.name, b.name

MATCH (a)-[r:KNOWS|WORKS_AT]->(b)
RETURN a.name, type(r), b.name

MATCH (a {name: 'Alice'})
MATCH (a)-[:KNOWS]->(b)
RETURN b.name
  • (n:Person:Employee) matches nodes that have both labels.
  • (a)-[e]-(b) (or (a)--(b), (a)<-[e]->(b), (a)<-->(b)) ignores direction.
  • [:KNOWS|WORKS_AT] matches either edge type. An edge has exactly one type, so each edge pattern takes a single type or a list of alternatives.
  • type(r) returns the edge type as a string.
  • Several MATCH clauses in a row are allowed, and a later one can reuse variables from an earlier one.
  • Variables are only needed for entities you want to return or reuse. MATCH (:Person)-[:KNOWS]->(m) RETURN m.name is valid, and a node or edge may omit its variable when it has a label or type.

RETURN n returns the node’s internal ID, not its properties. Return the properties you need (RETURN n.name, n.age). This is different from Neo4j, where RETURN n returns the whole node.

​
Label expressions

Label expressions combine labels with | (or), & (and) and ! (not), on nodes and on edge types:

MATCH (n:Person|Company)
RETURN n.name

MATCH (n:!Person)
RETURN n.name

MATCH (n:Person&Employee)
RETURN n.name

MATCH (n:(Person&!Employee)|Company)
RETURN n.name

MATCH (a)-[r:!KNOWS]->(b)
RETURN type(r), count(*)

MATCH (p:Person)
OPTIONAL MATCH (p)-[:WORKS_AT]->(c:Company|Interest)
RETURN p.name, c.name

MATCH (n)
WHERE NOT n:Person|Company
RETURN n.name
  • Label expressions work in MATCH and OPTIONAL MATCH patterns and as tests in WHERE (WHERE n:Person|Company).
  • For edges, [:KNOWS|WORKS_AT] also works on variable-length hops: -[:KNOWS|LIKES]->+.
  • A label or an edge type can come from a query parameter, on its own or inside an expression: MATCH (n:Person|$other).
  • In CREATE and MERGE, labels are joined with : or & only, since a new node needs definite labels.

​
Filtering inside the pattern

A WHERE can sit inside a node or edge pattern. The variable must be named:

MATCH (a:Person WHERE a.age > 30)
  -[r:KNOWS WHERE r.since > 2012]->(b)
RETURN b.name

​
Ordering and limiting in MATCH

As a TuringDB extension, ORDER BY, SKIP and LIMIT can also appear right after the pattern, before RETURN:

MATCH (n:Person)
ORDER BY n.age LIMIT 2
RETURN n.name

Unknown labels and property names are forgiving: MATCH (n:Nope) RETURN n returns no rows and RETURN n.misspelled returns null. Use CALL db.labels(), CALL db.edgeTypes() and CALL db.propertyTypes() to check what exists in a graph.

​
OPTIONAL MATCH

OPTIONAL MATCH keeps the row when the pattern has no match and returns null for its variables:

MATCH (a:Person)
OPTIONAL MATCH (a)-[:WORKS_AT]->(c:Company)
RETURN a.name, c.name

MATCH (a:Person)
OPTIONAL MATCH (a)-[:KNOWS]->(b)
RETURN a.name, count(b)

The second query returns 0 for people who know nobody. A WHERE attached to an OPTIONAL MATCH belongs to the optional part, so rows are kept, as in Neo4j.

An unmatched node or edge returned on its own (RETURN c) is null, like its properties.

​
WHERE and operators

MATCH (n)
WHERE n:Person
RETURN n.name

MATCH (n:Person)
WHERE n.age >= 18 AND n.city = 'London'
RETURN n.name

MATCH (n:Person)
WHERE n.name STARTS WITH 'A'
  OR n.name ENDS WITH 'e'
  OR n.name CONTAINS 'ro'
RETURN n.name

MATCH (n:Person)
WHERE n.name IN ['Alice', 'Bob']
RETURN n.name

MATCH (n)
WHERE n IS Company
RETURN n.name

MATCH (n:Person)
WHERE 'admin' IN n.tags
RETURN n.name

MATCH (n:Person)
WHERE n.tags IS NOT NULL
RETURN n.name

MATCH (n:Person)
WHERE n.active
RETURN n.name

MATCH (n)-[e]->(m)
WHERE e:KNOWS AND e.since > 2012
RETURN n.name, m.name

MATCH (p:Person)
WHERE (p)-[:LIKES]->()
RETURN p.name

MATCH (p:Person)
WHERE NOT (p)-[:KNOWS]->()
RETURN p.name

MATCH (p:Person)
RETURN p.name,
  exists((p)-[:WORKS_AT]->(:Company {name: 'Acme'})) AS atAcme
  • Label and type tests: n:Person tests a node label, e:KNOWS tests an edge type, and n IS Person is the same test written with IS. Combine them with AND, OR and NOT, or write a label expression such as n:Person|Company or n IS Person|Company. Negate an IS test with NOT n IS Person.
  • Pattern predicates: WHERE (p)-[:LIKES]->() keeps the nodes for which the pattern exists, and exists((p)-[:LIKES]->()) is the same test as a value you can return. For richer conditions see EXISTS subqueries.
Operators
Comparison=, <>, !=, <, <=, >, >=, and chained comparisons such as 1 < x < 3
Null testsIS NULL, IS NOT NULL
Label testsn:Person, n IS Person, label expressions such as n:Person|Company
BooleanAND, OR, XOR, NOT, with three-valued logic for null as in Neo4j
Membership and stringsIN, STARTS WITH, ENDS WITH, CONTAINS, IS [NFC|NFD|NFKC|NFKD] NORMALIZED
Arithmetic+, -, *, /, %, ^. Integer / truncates and ^ returns a Double
Concatenation+ and || join strings and lists, and list + x appends an element

Property constraints in a pattern, MATCH (n {name: 'Matt', age: 20}), are exactly equivalent to the same equality tests in WHERE.

Use n.prop IS NOT NULL to test for a property: exists() takes a pattern, so exists(n.prop) is reported with that hint, as in Neo4j 5. Use STARTS WITH, ENDS WITH and CONTAINS to match strings. Regular expressions (=~) are not yet supported.

​
Comparing numbers

Integers and Doubles compare by value, so an Integer 30 equals the Double 30.0:

MATCH (n:Person)
WHERE n.age = 30.0
RETURN n.name

MATCH (n:Person)
WHERE n.age IN [30.0, 35]
RETURN n.name

To match one Double against another, use a range. = between two Doubles is reported when the query is analysed, because of rounding:

MATCH (n:Person)
WHERE n.age * 1.1 > 32.9 AND n.age * 1.1 < 33.1
RETURN n.name

Mixed-type <, > and arithmetic work as expected, and an ordering between values with no common order, such as 1 < 'a', is null. Division by zero returns an execution error, for Doubles too, and Int64 arithmetic wraps around on overflow.

​
Joins and Cartesian products

Comma-separated patterns in a MATCH produce either a join or a Cartesian product, depending on whether they share a variable.

// Cartesian product: every Person with every Company (N x M rows)
MATCH (p:Person), (c:Company)
RETURN p.name, c.name

// Join: a shared variable links the patterns
MATCH (a:Person)-[:KNOWS]->(b), (b)-[:WORKS_AT]->(c)
RETURN a.name, b.name, c.name

// Join on a shared node: people who like the same thing
MATCH (a:Person)-->(i:Interest)<--(b:Person)
WHERE a <> b
RETURN a.name, b.name, i.name

// Join on a value through WHERE (hash join)
MATCH (a:Person), (b:Person)
WHERE a.city = b.city AND a <> b
RETURN a.name, b.name

A Cartesian product of N nodes by M nodes produces N × M rows. Always constrain it with WHERE or LIMIT before running it on a large graph.

Connected paths and independent patterns can be mixed in one query. Shared variables create joins and unrelated patterns create Cartesian products:

MATCH (a:Person)-->(i:Interest), (c:Company)
WHERE c.name = 'Acme'
RETURN a.name, i.name, c.name

​
Look up nodes by native node ID

Every node has a native node ID, the integer you get from RETURN n or id(n). When you already know which nodes you want, compare the node with its ID, WHERE n = 123 or WHERE id(n) = 123. TuringDB then looks those nodes up directly instead of scanning the graph:

MATCH (n)
WHERE n = 0
RETURN n.name

MATCH (n)-[:KNOWS]->(m)
WHERE id(n) = 0
RETURN m.name

MATCH (n)
WHERE n IN [0, 1, 2]
RETURN n.name

UNWIND [0, 1, 2] AS x
MATCH (n)
WHERE n = x
RETURN x, n.name

UNWIND [[0, 1], [2, 3]] AS p
MATCH (a)
WHERE a = p[0]
MATCH (b)
WHERE b = p[1]
RETURN a.name, b.name

EXPLAIN shows the lookup: the plan contains const_scan_nodes for IDs written in the query, or fetch_nodes for IDs read from a row or a parameter, rather than scan_nodes:

EXPLAIN MATCH (n)
WHERE n = 0
RETURN n.name

IDs in parameters. With query parameters, compare the node itself with the ID. WHERE n = $id looks the node up directly, while WHERE id(n) = $id and WHERE n IN $ids check every node. UNWIND a list of IDs:

client.query("MATCH (n) WHERE n = $id RETURN n.name", {"id": 0})
client.query(
    "UNWIND $ids AS x MATCH (n) WHERE n = x RETURN x, n.name",
    {"ids": [0, 1, 2]},
)
  • Batches. UNWIND a list of IDs and compare each one: UNWIND $ids AS x MATCH (n) WHERE n = x. The list can be a parameter, written in the query, held in a variable, or built with collect() or range(), and each ID costs a few microseconds. A list written in the query also works as WHERE n IN [0, 1, 2].
  • Several IDs per row. Two IDs from the same row work too, which is the fast way to create many edges between existing nodes.
  • Fetch IDs once and reuse them. Look nodes up with whatever filter you need (MATCH (n:Person {name: 'Alice'}) RETURN id(n)), keep the IDs in your application, and address the nodes by ID from then on. Looking each row’s node up through a key property such as n.uuid reads every node with that label instead, and is about 100 times slower on a large graph.
  • Vector search results are already nodes. VECTOR SEARCH ... YIELD ids binds ids as nodes, so use ids.title or MATCH (ids)-[...]->(...) directly. See Vector search.

Stability. The native IDs of existing nodes are stable across queries, change submits and server restarts, so it is safe to cache them. Two exceptions:

  • Nodes created in an open change get their final IDs at COMMIT or CHANGE SUBMIT. Read them after that.
  • MERGE_DATAPARTS renumbers all nodes. Re-fetch any cached IDs afterwards.

Keep a business key as a property as well, for durable identity outside TuringDB. Use id(n) for node IDs. For edges, MATCH ()-[r]->() WHERE id(r) = 0 RETURN startNode(r), endNode(r) works.

OverviewPaths
githublinkedinyoutubediscord