Node patterns are a core concept in Cypher, defining how to match or create data within the graph. Understanding how to create and use patterns will allow you to write expressive queries that can efficiently retrieve and manipulate your graph data.
Relationship Patterns
Relationship patterns describe how nodes are connected through relationships. A typical relationship pattern consists of two nodes and the relationship between them. For example:
MATCH (a:Person)-[:KNOWS]->(b:Person)
RETURN a, b
This returns a row for each KNOWS relationship directed from a person a to a person b. Use (a:Person)<-[:KNOWS]-(b:Person) to match relationships directed from b to a. Use (a:Person)-[:KNOWS]-(b:Person) to match either direction.
Relationship patterns can also include a variable for the relationship, allowing you to return or manipulate the relationship itself:
MATCH (a:Person)-[rel:KNOWS]->(b:Person)
RETURN a, rel, b
Specify alternative relationship types with |, as in -[:KNOWS|WORKS_WITH]->. A node pattern such as (n:Person:Employee) requires both labels and also matches nodes with additional labels. Use backticks to quote identifiers that contain spaces or punctuation, including variables, labels, relationship types, property keys, and aliases.
Quote reserved words when you use them as variable names or aliases. This also applies to named parameters. In this example, starts is a property key and the output alias is quoted.
WITH {starts: true} AS event
RETURN event.starts AS `starts`
Use the same label syntax as a predicate on a bound node. This query returns people who also have the Employee label.
MATCH (p)
WHERE p:Person:Employee
RETURN p.name AS employee
Variable Length Relationships
A variable-length relationship pattern matches a sequence of relationships. For example, find people connected by two to five outgoing KNOWS relationships and return the relationships in traversal order.
MATCH (a:Person)-[links:KNOWS*2..5]->(b:Person)
RETURN a.name AS origin, b.name AS destination, links
| Length syntax | Number of relationships |
|---|---|
[*] |
One or more |
[*0] |
Exactly zero |
[*2] |
Exactly two |
[*2..5] |
Two to five, inclusive |
[*2..] |
Two or more |
[*..5] |
One to five |
[*0..5] |
Zero to five |
Omitted lower bounds default to one. An explicit zero allows a match where both endpoints are the same node and the relationship list is empty, provided the node satisfies both endpoint patterns. An interval with its upper bound below its lower bound has no matches. OPTIONAL MATCH still preserves the input row for an empty interval.
Bounds are non-negative integer literals. They accept decimal, hexadecimal with 0x, and octal with 0o, as in [*0x2..0o5]. A finite upper bound limits the traversal when you know the maximum distance.
A property map on a variable-length relationship constrains every relationship in the sequence. For example, -[:KNOWS* {since: 2020}]-> matches sequences in which each relationship has the property since set to 2020.
Within one MATCH or OPTIONAL MATCH clause, a relationship can be used once across all comma-separated patterns. Node variables with the same name identify the same node. Separate MATCH clauses can reuse relationships. A variable such as links in a quantified pattern holds a relationship list even if it contains just one relationship. When reused, it matches that list in its bound order.
Repeating a quantified relationship binding within the same clause makes the combined pattern fail. For each Alice matched here, OPTIONAL MATCH preserves the row and sets both links and b to null.
MATCH (a:Person {name: 'Alice'})
OPTIONAL MATCH (a)-[links:KNOWS*]->(b), (a)-[links:KNOWS*]->(b)
RETURN a.name AS person, links, b
Path Expressions
Name an entire pattern to return a path value. The path records the nodes and relationships in traversal order.
MATCH p = (:Person {name: 'Alice'})-[:KNOWS*]->(:Person)
RETURN p
Use nodes(p), relationships(p), and length(p) to inspect the path. Endpoint labels constrain only the endpoints. A zero-hop path contains one node and no relationships. Use shortestPath() or allShortestPaths() to choose paths with the fewest hops.
Pattern Comprehensions
A pattern comprehension returns a list by evaluating a projection for each matching path. It can refer to bound variables from the current row. Use WHERE inside the comprehension to filter its matches. A pattern with no qualifying matches produces an empty list.
MATCH (p:Person {name: 'Alice'})
RETURN [(p)-[:KNOWS]->(friend:Person) WHERE friend.active | friend.name] AS activeFriends
Whole-Graph Scan
MATCH (*) RETURN * reads the graph’s nodes and relationships. Run it as a standalone statement. Use ordinary patterns to apply filters or compose a query with other clauses.