Neo4j Guide
Cypher Syntax Reference
-- Node: (variable:Label {property: value})
-- Relationship: -[:TYPE {property}]->
-- Pattern: (n)-[:KNOWS]->(m)
-- CREATE nodes and relationships
CREATE (alice:Person {name: 'Alice', age: 30})
CREATE (bob:Person {name: 'Bob', age: 25})
CREATE (alice)-[:KNOWS {since: 2020}]->(bob)
-- MERGE (create if not exists)
MERGE (p:Person {email: '[email protected]'})
ON CREATE SET p.created = timestamp()
ON MATCH SET p.lastSeen = timestamp()
-- MATCH with pattern
MATCH (alice:Person {name: 'Alice'})-[:KNOWS]->(friend)
RETURN friend.name, friend.age
-- Multi-hop path
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person)
WHERE a.name = 'Alice'
RETURN DISTINCT b.name
-- Shortest path
MATCH path = shortestPath(
(a:Person {name:'Alice'})-[:KNOWS*]-(b:Person {name:'Charlie'})
)
RETURN length(path), nodes(path)
-- Aggregation
MATCH (p:Person)-[:BOUGHT]->(product)
RETURN product.name, count(*) AS buyers, sum(product.price) AS revenue
ORDER BY revenue DESC LIMIT 10
Use Cases for Graph Databases
| Use Case | Why Graph Wins |
|---|---|
| Social Networks | Friends of friends queries are O(1) per hop |
| Recommendation Engines | Traverse shared interests efficiently |
| Fraud Detection | Find connected fraud rings |
| Knowledge Graphs | Semantic relationships |
| Supply Chain | Complex dependency tracking |