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 CaseWhy Graph Wins
Social NetworksFriends of friends queries are O(1) per hop
Recommendation EnginesTraverse shared interests efficiently
Fraud DetectionFind connected fraud rings
Knowledge GraphsSemantic relationships
Supply ChainComplex dependency tracking