🔹 Relationship Direction
In Neo4j, relationships are directed (→ or ←).
But whether direction matters depends on your query.
Example: Explicit Direction
👉 Finds only projects where Alice works on the project.
If you reverse:
👉 Looks for projects that work on Alice (usually makes no sense).
Example: Ignore Direction
If you don’t care about direction:
👉 Matches regardless of arrow direction.
Useful when relationships are logically undirected (like "FRIENDS_WITH").
📌 Rule of thumb:
-
Use direction (
-->
) when the semantics matter (likeUSER -> WORKS_ON -> PROJECT
). -
Drop direction (
--
) if it doesn’t matter.
🔹 OPTIONAL MATCH
This is like a LEFT JOIN in SQL.
It tries to match a pattern, but if it’s missing, it still returns the node with null
for missing parts.
Example:
👉 If Alice works on ProjectX → returns ("Alice", "ProjectX")
👉 If Alice has no project → returns ("Alice", null)
Why it’s useful?
-
To include users without projects.
-
To include apps without owners.
-
To handle missing data gracefully.
⚡ Quick Summary
Concept | What it does |
---|---|
Direction (a)-[:REL]->(b) | Only matches if direction matches |
No direction (a)-[:REL]-(b) | Matches both directions |
OPTIONAL MATCH | Returns result even if no match (like SQL LEFT JOIN) |
Let’s go step by step on Pattern Matching Basics in Neo4j.
🔹 Pattern Matching in Cypher
Neo4j queries are written in terms of patterns made of:
-
Nodes:
()
-
Relationships:
-[]-
-
Direction:
->
or<-
Think of it like drawing a graph with text.
1. Node Only
👉 Matches all nodes with label User
.
2. Node with Relationship
👉 Matches a User connected by a WORKS_ON
relationship to a Project.
3. Relationship Properties
👉 Finds relationships where the since
property = 2022
.
4. Ignore Relationship Direction
👉 Matches both (User)-[:WORKS_ON]->(Project)
and (User)<-[:WORKS_ON]-(Project)
.
5. Longer Patterns
👉 Follows a chain: User → Project → Application.
📌 Pattern syntax in general:
-
(node)
→ Node -
[:TYPE]
→ Relationship type -
->
or<-
→ Direction
✅ So ()-[]->()
just means:
"A node connected to another node via a directed relationship.
let’s dive into Aggregations in Neo4j (Cypher).
Aggregations are used when you want to group and summarize data — just like GROUP BY
in SQL.
🔹 Common Aggregation Functions
1. COUNT() – Count nodes or relationships
👉 Returns each project’s name and the number of users working on it.
2. COLLECT() – Gather values into a list
👉 Groups all users for each project into a list.
Example result:
3. SUM() – Add numeric values
👉 Calculates total salary of users per project.
4. AVG() – Average numeric values
👉 Finds average years of experience of users per project.
5. MIN() / MAX()
👉 Returns youngest and most experienced team member for each project.
🔹 GROUPING in Cypher
Unlike SQL’s GROUP BY
, Cypher automatically groups by the non-aggregated values in your RETURN
.
Example:
👉 Here p.name
is the grouping key.
Each project gets its count + list of users.
🔹 Combining Aggregations
You can mix multiple:
Comments
Post a Comment