Neo4j

Native graph database - relationship-first modeling, powerful traversals, Cypher query language

TL;DR

What: A native graph database for connected data.

Why: Relationship-first, intuitive data modeling, powerful traversals, Cypher query language.

Quick Start

Install with Docker:

docker run --name neo4j -p 7474:7474 -p 7687:7687 \
  -e NEO4J_AUTH=neo4j/password \
  -d neo4j:latest

Access: Open http://localhost:7474 (Browser UI)

Connect: Use neo4j / password

Create nodes and relationships:

CREATE (john:Person {name: 'John', age: 30})
CREATE (jane:Person {name: 'Jane', age: 28})
CREATE (john)-[:KNOWS]->(jane)
RETURN john, jane

Cheatsheet

OperationCypher
Create nodeCREATE (n:Label {props})
Create relationshipCREATE (a)-[:TYPE]->(b)
Find nodesMATCH (n:Label) RETURN n
Find by propertyMATCH (n {name: 'John'}) RETURN n
Find relationshipsMATCH (a)-[r:TYPE]->(b) RETURN r
UpdateSET n.prop = value
DeleteDELETE n or DETACH DELETE n

Gotchas

Basic CRUD

// Create
CREATE (p:Person {name: 'Alice', email: '[email protected]'})

// Read
MATCH (p:Person {name: 'Alice'}) RETURN p

// Update
MATCH (p:Person {name: 'Alice'})
SET p.age = 25
RETURN p

// Delete (with relationships)
MATCH (p:Person {name: 'Alice'})
DETACH DELETE p

Relationship patterns

// Directed relationship
MATCH (a:Person)-[:KNOWS]->(b:Person)
RETURN a, b

// Undirected (any direction)
MATCH (a:Person)-[:KNOWS]-(b:Person)
RETURN a, b

// Multiple hops
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person)
RETURN a, b

// Variable-length path
MATCH path = (a:Person)-[:KNOWS*]->(b:Person)
RETURN path

Aggregations

// Count friends
MATCH (p:Person)-[:KNOWS]->(friend)
RETURN p.name, count(friend) as friendCount

// Group by
MATCH (p:Person)
RETURN p.city, count(p) as population
ORDER BY population DESC

Indexes

// Create index
CREATE INDEX FOR (p:Person) ON (p.name)

// Create constraint (unique)
CREATE CONSTRAINT FOR (p:Person) REQUIRE p.email IS UNIQUE

// Show indexes
SHOW INDEXES

Next Steps