The below are some common examples of openCypher Syntax based upon the air-routes dataset provided by Kelvin Lawrence.
- Read Queries
- Write Queries
- Operators
- Functions
Pattern matching is the most basic actions one can do in openCypher and is the basis for all read queries.
MATCH (a:airport) RETURN a
MATCH (a:airport {code: 'SEA'}) RETURN a
MATCH (a:airport) WHERE a.code='SEA' RETURN a
MATCH (a:airport {code: 'SEA'})-[:route]->(d) RETURN d
MATCH (a:airport {code: 'SEA'})<-[:route]-(d) RETURN d
MATCH p=(a:airport {code: 'SEA'})-[:route]->(d) RETURN p
OPTIONAL MATCH (a:airport {code: 'SEA'})-[:route]->(d) RETURN d
MATCH (a:airport {code: 'SEA'})-[:route]->(d) RETURN d
Looping through connections is a common pattern in property graphs and can be accomplished using either fixed or variable length paths.
MATCH (a:airport {code: 'SEA'}*2)-[:route]->(d) RETURN d
MATCH (a:airport {code: 'SEA'})-[:route*1..3]->(d) RETURN d
MATCH (a:airport {code: 'SEA'})-[:route*1..]->(d) RETURN d
MATCH (a:airport {code: 'SEA'})-[:route*..3]->(d) RETURN d
Each read query must specify how to return values from the query.
MATCH (a) RETURN *
MATCH (a:airport) RETURN a.city
MATCH (a:airport) RETURN a.city AS dest
MATCH (a:airport) RETURN DISTINCT a.region
MATCH (a:airport) RETURN a ORDER BY a.elev
MATCH (a:airport) RETURN a ORDER BY a.elev DESC
MATCH (a:airport) RETURN count(a)
MATCH (a:airport) RETURN a LIMIT 5
MATCH (a:airport) RETURN a SKIP 5 LIMIT 5
MATCH (a:airport {code:'SEA'})
RETURN a.city
UNION
MATCH (a:airport {code:'ANC'})
RETURN a.elev
MATCH (a:airport {code:'SEA'})-->(d)
RETURN d.city
UNION ALL
MATCH (a:airport {code:'ANC'})-->(d)
RETURN d.city
CREATE (a)
CREATE (a:airport)
CREATE (a:airport {code: 'FOO'})
MATCH (a:airport {code:'SEA'}), (b:airport {code:'ANC'})
CREATE (a)-[r:route]->(b)
RETURN r
MATCH (a:airport {code:'SEA'}), (b:airport {code:'ANC'})
CREATE (a)-[r:route {dist: 1000}]->(b)
RETURN r
CREATE p = (a:airport {code:'Foo'})-[:route]->(a:airport {code:'Bar'})
RETURN p
UNWIND [{code: 'foo'}, {code: 'bar'}] AS properties
CREATE (a:airport) SET a = properties
MATCH (a:airport {code: 'SEA'})
SET a.foo='bar'
MATCH (a:airport {code: 'SEA'})
SET a.foo=null
MATCH (a:airport {code: 'SEA'})
REMOVE a.foo
MERGE (a:airport {code: 'SEA'})
ON CREATE SET n.created = timestamp()
ON MATCH SET
n.counter = coalesce(n.counter, 0) + 1,
n.accessTime = timestamp()
DELETE (a:airport {code: 'SEA'})
MATCH (a:airport {code: 'SEA'})
DETACH DELETE a
MATCH (a:airport {code:'SEA'})-[r]->(d)
DELETE r
| Type | Operators |
|---|---|
| General | DISTINCT, x.y (property access) |
| Math | +, -, *, /, %, ^ |
| Comparison | =, >, <, <>, <=, >=, IS NULL, IS NOT NULL |
| Boolean | AND, OR, NOT, XOR |
| String | STARTS WITH, ENDS WITH, CONTAINS, + |
| LIST | +, IN, [] |
| Type | Functions |
|---|---|
| Predicate | exists() |
| Scalar | coalesce(), endNode(), head(), id(0, last(), length(), properties(), size(), startNode(), timestamp(), toBoolean(), toFloat(), toInteger(), type() |
| Aggregating | avg(), collect(), count(), max(), min(), percentileCont(), percentileDisc(), stDev(), stDevP(), sum() |
| List | keys(), labels(), nodes(), range(), relationships(), reverse(), tail() |
| Math - numeric | abs(), ceil(), floor(), rand(), round(), sign() |
| Math - logarithmic | e(), exp(), log(), log10(), sqrt() |
| Math - trigonometric | acos(), asin(), atan(), atan2(), cos(), cot(), degree(), pi(), radians(), sin(), tan() |
| String | left(), lTrim(), replace(), reverse(), right(), rTrim(), split(), substring(), toLower(), toString(), toUpper(), trim() |