Neo4j

Base de donnees graphe native - langage Cypher, modelisation relationnelle, traversees rapides

TL;DR

Quoi : Une base de données graphe native pour les données connectées.

Pourquoi : Orientée relations, modélisation intuitive, traversées puissantes, langage de requête Cypher.

Quick Start

Installer avec Docker :

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

Accès : Ouvrez http://localhost:7474 (Interface navigateur)

Connexion : Utilisez neo4j / password

Créer des nœuds et relations :

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

Cheatsheet

OpérationCypher
Créer un nœudCREATE (n:Label {props})
Créer une relationCREATE (a)-[:TYPE]->(b)
Trouver des nœudsMATCH (n:Label) RETURN n
Trouver par propriétéMATCH (n {name: 'John'}) RETURN n
Trouver des relationsMATCH (a)-[r:TYPE]->(b) RETURN r
Mettre à jourSET n.prop = value
SupprimerDELETE n ou 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