Cypher

#syntax/cypher

Patterns

PatternDescription
(n)-[r]->(m)Bind to relation r
(n)-[*]->(m)Variable length relationships
(n)-[*1..5]->(m)Variable length path of 1-5 relationships
(n)-[*0..]->(m)Recursive Relationship
(n)-[:KNOWS|:LOVES]-(m)match Relationship Type using OR
(me:Me:User)match multiple Labels using AND
  • Supports Aliases in Return clause: MATCH(o:Org) RETURN o.name as OrgName
  • Short Where Clause: MATCH (p:Person{name: 'Frank'}) RETURN p.name
  • Avoid duplicates with RETURN DISTINCT
  • Optional Match with OPTIONAL MATCH(o:Org)
  • Similar to SQL joins, Cypher has *0..
  • Negate Pattern with NOT EXISTS( (n)--(m) ) or isEmpty( (n)-->(m) )
  • Remove Nodes WITHOUT Relationships MATCH(n) DELETE n
  • Remove Nodes WITH Relationships MATCH(n) DETACH DELETE n
  • Create or Update with MERGE (u:User{name:"fab"})
  • Inequality Comparison with <>

Examples

MATCH (o) RETURN o; // find pattern in graph
 
MATCH (o:Org) RETURN o; // find node label Org
 
// find all offices that are located in city
MATCH (o:Office)-[r:IS_LOCATED_IN]->(c:CITY) RETURN o, r, c;
// or
MATCH p = (:Office)-[:IS_LOCATED_IN]->(:CITY) RETURN p;
 
// find frank
MATCH (p:Person)
WHERE p.name = 'Frank'
RETURN p.name
 
// Regular Expression
MATCH (d:Dept)-->(o:Org)
WHERE d.name =~ 'Gra.*'
RETURN d.name, 
  collect(distinct o.name) as OrgNames // collect to list
 
// Starts with & Count
MATCH (d:Dept)
WHERE d.name STARTS WITH 'Gra'
RETURN d.name, count(*)
ORDER BY d.name DESC
SKIP 3 // skip first 3 entries
LIMIT 5 // limit to first 5 entries