Neo4j Cypher Cheatsheet - Neo4j Graph Database Command Reference
All essential Neo4j Cypher commands organized by use case, with 40+ entries you can copy and run directly. Find the right command fast when you need it.
Back to DatabasesCreate Nodes & Relationships 5
CREATE (n:Person {name: "Alice"})Create a node
CREATE (a:Person)-[:KNOWS]->(b:Person)Create a relationship
CREATE (a:Person {name: "Alice"})-[:KNOWS {since: 2020}]->(b:Person {name: "Bob"})Create node & relationship with properties
MERGE (n:Person {name: "Alice"})Match if exists, else create
MERGE (a)-[:KNOWS]->(b) ON CREATE SET a.created = timestamp()MERGE with onCreate
Query & Match 9
MATCH (n:Person) RETURN nMatch all Person nodes
MATCH (n:Person {name: "Alice"}) RETURN nMatch by property
MATCH (n:Person) WHERE n.age > 18 RETURN nWHERE filter
MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a, bMatch a relationship
MATCH (a:Person)-[:KNOWS*2]->(b:Person) RETURN a, b2-hop relationship
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person) RETURN a, b1-3 hop relationship
MATCH (n) OPTIONAL MATCH (n)-[:KNOWS]->(m) RETURN n, mOptional match (null if no rel)
MATCH path = (a)-[*]->(b) RETURN pathMatch a path
MATCH path = shortestPath((a)-[:KNOWS*]->(b)) RETURN pathShortest path
WITH & Subqueries 5
MATCH (n:Person) WITH n WHERE n.age > 18 RETURN n.nameWITH filter & pass
MATCH (n:Person)-[:KNOWS]->(m) WITH n, count(m) AS friends RETURN n.name, friendsWITH aggregate & pass
UNWIND [1, 2, 3] AS x RETURN xUnwind array into rows
CALL { MATCH (n:Person) RETURN n ORDER BY n.name LIMIT 5 } RETURN count(*)CALL subquery
MATCH (n:Person) RETURN n.name AS name UNION MATCH (n:Company) RETURN n.name AS nameUNION merge results
Update & Delete 6
MATCH (n:Person {name: "Alice"}) SET n.age = 30Update a property
MATCH (n:Person {name: "Alice"}) SET n += {age: 30, city: "Beijing"}Update multiple props
MATCH (n:Person {name: "Alice"}) REMOVE n.ageRemove a property
MATCH (n:Person {name: "Alice"}) DELETE nDelete node (must have no rels)
MATCH (n:Person {name: "Alice"}) DETACH DELETE nDelete node and its relationships
MATCH (a)-[r:KNOWS]->(b) DELETE rDelete a relationship
Aggregation & Order 9
MATCH (n:Person) RETURN count(n)Count
MATCH (n:Person) RETURN DISTINCT n.cityDistinct
MATCH (n:Person) RETURN n.name, n.age ORDER BY n.age DESCOrder by
MATCH (n:Person) RETURN n.age, count(*) ORDER BY n.ageGroup & count
MATCH (n:Person) RETURN collect(n.name)Collect into array
MATCH (n:Person) RETURN avg(n.age), min(n.age), max(n.age)Aggregate functions
MATCH (n:Person) RETURN percentileCont(n.age, 0.5)Median (percentile)
MATCH (n:Person) RETURN n LIMIT 10Limit results
MATCH (n:Person) RETURN n SKIP 5 LIMIT 10Pagination
Index & Constraint 6
CREATE INDEX ON :Person(name)Create an index
CREATE CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUEUnique constraint
CREATE CONSTRAINT ON (n:Person) ASSERT exists(n.name)Existence constraint
DROP INDEX ON :Person(name)Drop an index
DROP CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUEDrop a constraint
CALL db.indexes()List all indexes
💡 Tips
- MATCH 查询前先用 EXPLAIN 查看执行计划,避免全图扫描。
- MERGE 比 CREATE 安全,避免重复创建节点。
- 长路径查询(*1..10)可能很慢,建议加 LIMIT 或用图算法。
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Found an error? Report it
Wrong command or description? Open an issue to help us fix it.
Found an error? Report it