Writing Data
CREATE
-- Create a node
CREATE (n:Person {name: "Alice", age: 30})
-- Create multiple nodes
CREATE (a:Person {name: "Alice"}), (b:Person {name: "Bob"})
-- Create with relationships
CREATE (a:Person {name: "Alice"})
CREATE (b:Person {name: "Bob"})
CREATE (a)-[:FRIEND {since: 2020}]->(b)
-- Create everything in one pattern
CREATE (a:Person {name: "Alice"})-[:WORKS_AT]->(c:Company {name: "Acme"})MERGE
Match-or-create semantics. Use ON CREATE SET and ON MATCH SET to conditionally set properties.
-- Create if not exists
MERGE (n:Person {name: "Alice"})
ON CREATE SET n.created = timestamp()
ON MATCH SET n.lastSeen = timestamp()
RETURN n
-- Merge relationships
MATCH (a:Person {name: "Alice"}), (b:Person {name: "Bob"})
MERGE (a)-[:FRIEND]->(b)SET
-- Set properties
MATCH (n:Person {name: "Alice"})
SET n.age = 31, n.email = "alice@example.com"
RETURN n
-- Add a label
MATCH (n:Person {name: "Alice"})
SET n:Employee
-- Copy all properties from a map
MATCH (n:Person {name: "Alice"})
SET n += {city: "Sydney", country: "AU"}REMOVE
-- Remove a property
MATCH (n:Person {name: "Alice"})
REMOVE n.email
-- Remove a label
MATCH (n:Person:Employee {name: "Alice"})
REMOVE n:EmployeeDELETE
-- Delete a node (must have no relationships)
MATCH (n:Person {name: "Bob"})
DELETE n
-- Delete node and all its relationships
MATCH (n:Person {name: "Bob"})
DETACH DELETE n
-- Delete a relationship
MATCH (a:Person)-[r:FRIEND]->(b:Person)
WHERE a.name = "Alice" AND b.name = "Bob"
DELETE r
-- Delete all nodes with a label
MATCH (n:TempData) DETACH DELETE nFOREACH
Iterate over a list and perform write operations for each element.
MATCH p = (a:Person {name: "Alice"})-[:FRIEND*]->(b)
FOREACH (n IN nodes(p) | SET n.visited = true)