Store any kind of data using the following graph concepts:
- Node: Graph data records
- Relationship: Connect nodes (has direction and a type)
- Property: Stores data in key-value pair in nodes and relationships
- Label: Groups nodes and relationships (optional)
Examples: :help
:clear
MATCH (ee:Person)
WHERE ee.name = "Emil"
RETURN ee;
- MATCH clause to specify a pattern of nodes and relationships
- (ee:Person) a single node pattern with label 'Person' which will assign matches to the variable
ee
- WHERE clause to constrain the results
- ee.name = "Emil" compares name property to the value "Emil"
- RETURN clause used to request particular results
Gets gets the id<5> and id<0> nodes and creates a :KNOWS
relationship between them
MATCH (ee:Person)-[:KNOWS]-(friends)
WHERE ee.name = "Emil"
RETURN ee, friends
- MATCH clause to describe the pattern from known Nodes to found Nodes
- (ee) starts the pattern with a Person (qualified by WHERE)
- -[:KNOWS]- matches "KNOWS" relationships (in either direction)
- (friends) will be bound to Emil's friends
MATCH (n:Person)
RETURN n
or
MATCH (n)
WHERE n:Person
RETURN n
:Car
OR :Person
labels
MATCH (n)
WHERE n:Person OR n:Car
RETURN n
:Car
AND :Person
labels
MATCH (n)
WHERE n:Person:Car
RETURN n
MATCH (a:Person)
WHERE a.from = "Sweden"
RETURN a
Returns every node (and their relationships) where there's a property from
with "Sweden" value
Johan is learning surfing, and wants to know any friend of his friends who already knows surfing
MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
WHERE js.name = "Johan" AND surfer.hobby = "surfing"
RETURN DISTINCT surfer
- () empty parenthesis to ignore these nodes
- DISTINCT because more than one path will match the pattern
- surfer will contain Allison, a friend of a friend who surfs
Every node and relationship has an internal autonumeric ID, which can be queried using <, <=, =, =>, <> and IN operators:
Search node by ID
MATCH (n)
WHERE id(n) = 0
RETURN n
Search multiple nodes by ID
MATCH (n)
WHERE id(n) IN [1, 2, 3]
RETURN n
Search relationship by ID
MATCH ()-[n]-()
WHERE id(n) = 0
RETURN n
CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })
- CREATE clause to create data
- () parenthesis to indicate a node
- ee:Person a variable
ee
and labelPerson
for the new node - {} brackets to add properties (key-value pairs) to the node
MATCH (ee:Person) WHERE ee.name = "Emil"
CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }),
(ir:Person { name: "Ian", from: "England", title: "author" }),
(rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }),
(ally:Person { name: "Allison", from: "California", hobby: "surfing" }),
(ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir),
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
(rvb)-[:KNOWS]->(ally)
- MATCH clause to get "Emil" in
ee
variable - CREATE clause to create multiple nodes (comma separated) with their labels and properties. Also creates directed relationships
(a)-[:Label {key: value}]->(b)
MATCH (n), (m)
WHERE n.name = "Allison" AND m.name = "Emil"
CREATE (n)-[:KNOWS]->(m)
Alternative with MERGE
, which ensures that the relationship is created only once
MATCH (n:User {name: "Allison"}), (m:User {name: "Emil"})
MERGE (n)-[:KNOWS]->(m)
CREATE (n:Actor:Director)
Add new .owns
property or modify (if exists)
MATCH (n)
WHERE n.name = "Rik"
SET n.owns = "Audi"
Danger: It will delete all previous properties and create .plays
and .age
properties
MATCH (n)
WHERE n.name = "Rik"
SET n = {plays: "Piano", age: 23}
Danger: If .plays
or .age
properties are already set, it will overwrite them
MATCH (n)
WHERE n.name = "Rik"
SET n += {plays: "Piano", age: 23}
MATCH (n)
WHERE n.plays = "Guitar" AND NOT (EXISTS (n.likes))
SET n.likes = "Movies"
MATCH (n)
WHERE NOT (EXISTS (n.instrument))
SET n.instrument = n.plays
REMOVE n.plays
Alternative
MATCH (n)
WHERE n.instrument is null
SET n.instrument = n.plays
REMOVE n.plays
Adds the :Food
label to nodes id<7> and id<8>
MATCH (n)
WHERE id(n) IN [7, 8]
SET n:Food
MERGE (n:Person {name: "Rik"})
SET n.owns = "Audi"
To delete a node (p.e. id<5>), first we need to delete its relationships. Then, the node can be deleted
MATCH (n)-[r]-()
WHERE id(n) = 5
DELETE r, n
To delete multiple nodes (must have their relationships previously deleted)
MATCH (n)
WHERE id(n) IN [1, 2, 3]
DELETE n
MATCH (n)
WHERE n:Person AND n.name = "Rik" AND n.plays is NOT null
REMOVE n.plays
Alternative
MATCH (n)
WHERE n:Person AND n.name = "Rik" AND EXISTS (n.plays)
REMOVE n.plays
Deletes the :Person
label from all nodes
MATCH (n)
REMOVE n:Person
Deletes the :Person
label from nodes with :Food
and :Person
labels
MATCH (n)
WHERE n:Food:Person
REMOVE n:Person
Deletes the :Food
and :Person
labels from nodes which have both labels
MATCH (n)
WHERE n:Food:Person
REMOVE n:Food:Person
Danger: Deletes the :Food
and :Person
labels from nodes which have :Food
or :Person
or :Food:Person
labels
MATCH (n)
REMOVE n:Food:Person
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n, r
Use PROFILE
or EXPLAIN
before the query
PROFILE
: Shows the execution plan, query information and db hits. Example: Cypher version: CYPHER 3.0, planner: COST, runtime: INTERPRETED. 84 total db hits in 32 ms.
EXPLAIN
: Shows the execution plan and query information. Example: Cypher version: CYPHER 3.0, planner: COST, runtime: INTERPRETED.
Count all nodes
MATCH (n)
RETURN count(n)
Count all relationships
MATCH ()-->()
RETURN count(*);
Returns up to 2 nodes (and their relationships) where there's a property from
with "Sweden" value
MATCH (a:Person)
WHERE a.from = "Sweden"
RETURN a
LIMIT 2
Make .name
property unique on nodes with :Person
label
CREATE CONSTRAINT ON (n:Person)
ASSERT n.name IS UNIQUE
Make .name
property unique on nodes with :Person
label
DROP CONSTRAINT ON (n:Person)
ASSERT n.name IS UNIQUE