[Avg. reading time: 9 minutes]
Neo4J Components
Neo4j stores data as a property graph model, which consists of four main components.
- Nodes
- Labels
- Properties
- Relationships
Each component has a distinct role in representing and querying connected data.
In Neo4j, you don’t define tables or schemas ahead of time. You directly create nodes (data points) and optionally assign them labels and properties. The structure emerges dynamically from your data.
Node
// Create a single node
CREATE (n)
// Create two arbitrary nodes
CREATE (a), (b)
// View all nodes
MATCH (n) RETURN n;
In Cypher, (n) represents a node, and n is a variable you can use to refer to that node later.
Label
// Node with Label
create(n:Student)
match(y) return y
Label every node. Nodes without Labels are not useful.
A label defines the type or category of a node. You can think of it like a table name in SQL, but more flexible, a node can have multiple labels.
// To name a Node after Creation. Example ID=2 set Label to Building
match(n) where ID(n)=2 set n:somelabel
// To rename a Label
match(n) where ID(n)=2 set n:Building remove n:somelabel
match(n) return n
Example: 1 Node has 2 labels Person & Student.
CREATE (p:Person:Student {name:"Monica", major:"CS"});
Properties
create (n:Book{title:"The Sapiens", author:"Yuval Noah"});
create (n:Book{title:"Who Moved My Cheese", author:"Johnson M.D"});
MATCH (b:Book) RETURN b.title, b.author;
match(t) return t
Relationships
Create relationships.
Create a relationship between the existing nodes.
Create a relationship with labels and properties.
CREATE (node1)-[:RelationshipType]->(node2)
CREATE (a:Person {name:"Rachel"})-[:FRIENDS_WITH]->(b:Person {name:"Ross"});
MATCH (p:Person)-[r]->(f:Person) RETURN p, r, f;
EXPLAIN provides the execution plan for the query without actually running it. This allows you to understand how Neo4j will execute your query (e.g., which indexes it might use) and see if there are any inefficiencies in your Cypher.
EXPLAIN MATCH (p:Person)-[r]->(f:Person) RETURN p, r, f;
PROFILE executes the query and gives you a detailed execution plan along with runtime statistics. It shows the actual execution steps taken by the database, including how many records were passed through each step, making it more comprehensive than EXPLAIN.
PROFILE MATCH (p:Person)-[r]->(f:Person) RETURN p, r, f;
Aspect | EXPLAIN | PROFILE |
---|---|---|
Execution | Does not run the query | Runs the query |
Performance Stats | No | Yes |
Use Case | Query plan analysis (safe, no data changes) | Actual performance analysis |
Impact | Low (no data fetching or mutation) | Potentially high, especially on large datasets |
Maintenance Commands
Detach and Delete all nodes
MATCH (n) DETACH DELETE n;
Delete all relationships
MATCH ()-[r]-() DELETE r;
Update all nodes
MATCH (n) SET n.active = true;