Professional Documents
Culture Documents
Neo4j Cypher Refcard Stable
Neo4j Cypher Refcard Stable
3
RETURN
INDEX
Patterns
Relationship Functions
Predicates
RETURN *
(n:Person)
type(a_relationship)
RETURN n AS columnName
startNode(a_relationship)
exists(n.property)
Use functions.
endNode(a_relationship)
(n:Person:Swedish)
Node with both Person and Swedish labels.
ORDER BY n.property
(n)-->(m)
id(a_relationship)
Relationship from n to m.
RETURN DISTINCT n
SKIP {skipNumber}
RETURN count(*)
MATCH
WITH
MATCH (user)-[:FRIEND]-(friend)
WHERE user.name = {name}
WITH user, count(friend) AS friends
WHERE friends > 10
RETURN user
The WITH syntax is similar to RETURN. It separates query
MATCH (n:Person)-[:KNOWS]->(m:Person)
WHERE n.name = "Alice"
UNION
CONSTRAINT
CREATE CONSTRAINT ON (p:Person)
ASSERT p.name IS UNIQUE
CREATE (n {collectionOfMaps})
(n)-[*1..5]->(m)
shortestPath((n1:Person)-[*..6]-(n2:Person))
allShortestPaths((n1:Person)-[*..6]->(n2:Person))
MATCH (a)-[:KNOWS]->(b)
RETURN b.name
UNION ALL
MATCH (a)-[:LOVES]->(b)
RETURN b.name
SET
Import
NULL
REMOVE n:Person
Remove a property.
FOREACH
Labels
CREATE (n:Person {name: {value}})
Operators
Collections
["a", "b", "c"] AS coll
Mathematical
+, -, *, /, %, ^
Comparison
Boolean
SET n:Spouse:Parent:Employee
String
Collection
+, IN, [x], [x .. y]
MATCH (n:Person)
Regular Expression
=~
String matching
collection.
the collection.
single(x IN coll WHERE exists(x.property))
Returns TRUE if the predicate is TRUE for exactly one
String matching.
coalesce(n.property, {defaultValue})
The first non-NULL expression.
n.property =~ "Tob.*"
timestamp()
(n)-[:KNOWS]->(m)
id(nodeOrRelationship)
NOT (n)-[:KNOWS]->(m)
Exclude matches to (n)-[:KNOWS]->(m) from the result.
toInt({expr})
Collection Expressions
toFloat({expr})
size({coll})
length(path)
MATCH (a)-[r:KNOWS*]->()
RETURN r AS rels
relationships(path)
coll[{idx}] AS value,
coll[{startIdx}..{endIdx}] AS slice
nodes(path)
Mathematical Functions
count(*)
round({expr})
count(DISTINCT identifier)
Round to the nearest integer, ceil and floor find the next
integer up or down.
sqrt({expr})
collect(n.property)
sign({expr})
0 if zero, -1 if negative, 1 if positive.
sum(n.property)
sin({expr})
percentileDisc(n.property, {percentile})
stdev(n.property)
String Functions
abs({expr})
MATCH (matchedNode:Person)
RETURN matchedNode
Collection Predicates
(n)-[*]->(m)
by Neo Technology.
(n)-[r]->(m)
FOREACH (r IN rels(path) |
SET r.marked = TRUE)
MERGE
(n)-[:KNOWS|:LOVES]->(m)
Relationship of type KNOWS or of type LOVES from n to m.
CREATE (n)-[r:KNOWS]->(m)
(m)<-[:KNOWS]-(n)
(n:Person)-->(m)
Node n labeled Person with relationship to m.
CREATE
n:Person
MATCH (a)-[:KNOWS]->(b)
RETURN b.name
UNION
MATCH (a)-[:LOVES]->(b)
RETURN b.name
(n)--(m)
LIMIT {limitNumber}
Syntax
MATCH (n:Person)
USING INDEX n:Person(name)
WHERE n.name = {value}
toString({expression})
Reverse a string.
length({string})
2.2.
CREATE UNIQUE
CREATE UNIQUE
(n)-[:KNOWS]->(m {property: {value}})
INDEX
Patterns
Relationship Functions
Predicates
RETURN *
(n:Person)
type(a_relationship)
RETURN n AS columnName
startNode(a_relationship)
exists(n.property)
Use functions.
endNode(a_relationship)
(n:Person:Swedish)
Node with both Person and Swedish labels.
ORDER BY n.property
(n)-->(m)
id(a_relationship)
Relationship from n to m.
RETURN DISTINCT n
SKIP {skipNumber}
RETURN count(*)
MATCH
WITH
MATCH (user)-[:FRIEND]-(friend)
WHERE user.name = {name}
WITH user, count(friend) AS friends
WHERE friends > 10
RETURN user
The WITH syntax is similar to RETURN. It separates query
MATCH (n:Person)-[:KNOWS]->(m:Person)
WHERE n.name = "Alice"
UNION
CONSTRAINT
CREATE CONSTRAINT ON (p:Person)
ASSERT p.name IS UNIQUE
CREATE (n {collectionOfMaps})
(n)-[*1..5]->(m)
shortestPath((n1:Person)-[*..6]-(n2:Person))
allShortestPaths((n1:Person)-[*..6]->(n2:Person))
MATCH (a)-[:KNOWS]->(b)
RETURN b.name
UNION ALL
MATCH (a)-[:LOVES]->(b)
RETURN b.name
SET
Import
NULL
REMOVE n:Person
Remove a property.
FOREACH
Labels
CREATE (n:Person {name: {value}})
Operators
Collections
["a", "b", "c"] AS coll
Mathematical
+, -, *, /, %, ^
Comparison
Boolean
SET n:Spouse:Parent:Employee
String
Collection
+, IN, [x], [x .. y]
MATCH (n:Person)
Regular Expression
=~
String matching
collection.
the collection.
single(x IN coll WHERE exists(x.property))
Returns TRUE if the predicate is TRUE for exactly one
String matching.
coalesce(n.property, {defaultValue})
The first non-NULL expression.
n.property =~ "Tob.*"
timestamp()
(n)-[:KNOWS]->(m)
id(nodeOrRelationship)
NOT (n)-[:KNOWS]->(m)
Exclude matches to (n)-[:KNOWS]->(m) from the result.
toInt({expr})
Collection Expressions
toFloat({expr})
size({coll})
length(path)
MATCH (a)-[r:KNOWS*]->()
RETURN r AS rels
relationships(path)
coll[{idx}] AS value,
coll[{startIdx}..{endIdx}] AS slice
nodes(path)
Mathematical Functions
count(*)
round({expr})
count(DISTINCT identifier)
Round to the nearest integer, ceil and floor find the next
integer up or down.
sqrt({expr})
collect(n.property)
sign({expr})
0 if zero, -1 if negative, 1 if positive.
sum(n.property)
sin({expr})
percentileDisc(n.property, {percentile})
stdev(n.property)
String Functions
abs({expr})
MATCH (matchedNode:Person)
RETURN matchedNode
Collection Predicates
(n)-[*]->(m)
by Neo Technology.
(n)-[r]->(m)
FOREACH (r IN rels(path) |
SET r.marked = TRUE)
MERGE
(n)-[:KNOWS|:LOVES]->(m)
Relationship of type KNOWS or of type LOVES from n to m.
CREATE (n)-[r:KNOWS]->(m)
(m)<-[:KNOWS]-(n)
(n:Person)-->(m)
Node n labeled Person with relationship to m.
CREATE
n:Person
MATCH (a)-[:KNOWS]->(b)
RETURN b.name
UNION
MATCH (a)-[:LOVES]->(b)
RETURN b.name
(n)--(m)
LIMIT {limitNumber}
Syntax
MATCH (n:Person)
USING INDEX n:Person(name)
WHERE n.name = {value}
toString({expression})
Reverse a string.
length({string})
2.2.
CREATE UNIQUE
CREATE UNIQUE
(n)-[:KNOWS]->(m {property: {value}})