Cypher
The openCypher subset that is supported, clause by clause.
Statements are openCypher as Neo4j 5 writes it. Keywords are not case-sensitive; variable, label, type and property names are. A name that is not a plain word is written in backticks.
Reading#
MATCH (a:Person {name: 'Ann'})-[r:KNOWS]->(b:Person)
WHERE r.since >= 2015 AND b.age > 30
RETURN b.name AS name, r.since ORDER BY name LIMIT 10| Clause | Supported |
|---|---|
MATCH, OPTIONAL MATCH | node patterns with several labels and a property map; relationship patterns -->, <--, --, with a variable, types (`[:A |
| Variable length | -[:T*]->, *2, *1..3, *..3, *2..; a variable on it is the list of relationships; no relationship is used twice in one match |
| Quantified path patterns | (s)((a)-[r:T]->(b)){1,3}(e), also +, *, {2}, {2,}, {,3}; several relationships and undirected groups too (upper bound at most 64 for those); the variables of the group are lists outside it; -[:T]->{1,3} without parentheses |
| Paths | p = (a)-[:T*1..3]->(b), length(p), nodes(p), relationships(p) |
| Shortest paths | shortestPath((a)-[:T*..10]->(b)) and allShortestPaths(...) between two nodes that are bound; at most one relationship type; start and end must differ |
WITH, RETURN | aliases, *, DISTINCT, ORDER BY with several keys and DESC, SKIP, LIMIT, WITH ... WHERE |
UNWIND | a list into rows |
UNION, UNION ALL | the column names of the parts must be equal |
CALL | the procedures db.labels, db.relationshipTypes, db.propertyKeys, db.ping, dbms.components, dbms.showCurrentUser, with or without YIELD |
Aggregates: count(*), count, sum, avg, min, max, collect, each also with DISTINCT. Every item without an aggregate is a grouping key.
Writing#
CREATE (a:Person {name: 'Ann'})-[:KNOWS {since: 2020}]->(b:Person {name: 'Bob'})
MERGE (c:City {name: 'Oslo'}) ON CREATE SET c.created = timestamp() ON MATCH SET c.seen = true
MATCH (a:Person {name: 'Ann'}) SET a.age = 31, a:Employee REMOVE a.temp
MATCH (a:Person {name: 'Bob'}) DETACH DELETE a
UNWIND $rows AS row CREATE (:Person {id: row.id, name: row.name})| Clause | Supported |
|---|---|
CREATE | nodes, relationships (one type, a direction), paths; properties from a map or a parameter |
MERGE | one pattern part; ON CREATE SET, ON MATCH SET |
SET | n.p = value (null removes the property), n = map, n += map, n:Label; the same on relationships |
REMOVE | n.p, n:Label |
DELETE, DETACH DELETE | nodes, relationships, paths; deleting a node that still has relationships is an error without DETACH |
A statement that fails changes nothing.
Expressions#
- Literals: integers (also
0x1F), floats, strings in single or double quotes with\escapes,true,false,null, lists[1, 2], maps{k: v}. - Parameters:
$name. - Operators:
+ - * / % ^,= <> < <= > >=(also chained:1 < x < 5),AND OR XOR NOT,IS NULL,IS NOT NULL,IN,STARTS WITH,ENDS WITH,CONTAINS,=~.=~supports literal text,.,.*and a leading(?i)only. n.property,map.key,list[i],list[a..b],n['property'],n:Labelas a test.CASE WHEN ... THEN ... ELSE ... ENDandCASE x WHEN ... THEN ... END.- List comprehension
[x IN list WHERE test | expr], pattern comprehension[(a)-->(b) WHERE test | expr],all,any,none,single,reduce, map projectionn {.name, .age, extra: expr}. - Patterns as tests:
WHERE (a)-[:KNOWS]->(b),exists((a)-->()),EXISTS { (a)-->(b) WHERE b.age > 30 },COUNT { (a)-->() }. nullfollows openCypher: it propagates through operators, andAND,OR,NOTuse three-valued logic.
Functions#
| Group | Functions |
|---|---|
| Graph | id, elementId, labels, type, properties, keys, startNode, endNode, nodes, relationships, length |
| Lists | size, head, last, tail, range, reverse, isEmpty, coalesce |
| Conversion | toInteger, toFloat, toString, toBoolean |
| Numbers | abs, ceil, floor, round, sign, sqrt, exp, log, log10, sin, cos, tan, asin, acos, atan, pi, e, rand |
| Text | toUpper, toLower, trim, ltrim, rtrim, substring, left, right, replace, split, size, reverse |
| Other | timestamp |
id(node) is the node's id in the graph. A relationship is identified by its start node, type and end node; id(relationship) is a number derived from them that is not stable across restarts. Use elementId or the nodes' own ids to refer to a relationship later.
Transactions#
Without a transaction, every statement is its own transaction and is durable when it is answered. Drivers open explicit transactions with their own calls (session.beginTransaction(), :begin in cypher-shell); in a script, BEGIN, COMMIT and ROLLBACK are statements.
- Inside a transaction the session sees its own writes; nobody else does until the commit.
- A transaction that has written is the graph's only writer until it ends: other writers wait for it. Readers never wait.
- An error ends the transaction: it is rolled back, and the next statements are refused until
ROLLBACK. - Label changes (
SET n:Label,REMOVE n:Label) are visible to other sessions at once, before the commit, and are taken back by a rollback. Everything else is isolated.
EXPLAIN and PROFILE#
EXPLAIN statement returns the plan without running the statement; PROFILE statement runs it and adds the row count and the time. The plan is a text with one line per clause and, under each MATCH, one line per step (NodeIndexSeek, NodeByLabelScan, Expand(All), Expand(Into), Intersect, VarLengthExpand, ShortestPath, Filter).