DEV Community

ZNY
ZNY

Posted on

The Complete Guide to Building with Neo4j and Cypher in 2026: Graph Databases for Connected Data

The Complete Guide to Building with Neo4j and Cypher in 2026: Graph Databases for Connected Data

Neo4j became the dominant graph database for applications with complex relationships — social networks, fraud detection, recommendation engines, and knowledge graphs. In 2025-2026, with AI knowledge graphs gaining traction, Neo4j skills are increasingly valuable.

Here's the practical guide.

Why Graph Databases

Relational DB: JOINs across many tables get slow as data grows
Graph DB: Relationships are first-class citizens, O(1) traversal
Enter fullscreen mode Exit fullscreen mode

For data where "friend of a friend of a friend" matters, graph wins.

Cypher Basics

-- Create nodes
CREATE (alice:Person {name: "Alice", age: 30})
CREATE (bob:Person {name: "Bob", age: 25})
CREATE (carol:Person {name: "Carol", age: 28})

-- Create relationships
CREATE (alice)-[:KNOWS]->(bob)
CREATE (bob)-[:KNOWS]->(carol)
CREATE (carol)-[:KNOWS]->(alice)
Enter fullscreen mode Exit fullscreen mode

Pattern Matching

-- Find Alice's friends
MATCH (alice:Person {name: "Alice"})-[:KNOWS]->(friend)
RETURN friend.name

-- Find friends of friends (2 hops)
MATCH (alice:Person {name: "Alice"})-[:KNOWS*2]->(fof)
RETURN fof.name

-- Find shortest path
MATCH path = shortestPath((alice:Person {name: "Alice"})-[:KNOWS*]-(carol:Person {name: "Carol"}))
RETURN path
Enter fullscreen mode Exit fullscreen mode

Complex Patterns

-- Find mutual friends
MATCH (alice:Person {name: "Alice"})-[:KNOWS]->(common)
MATCH (bob:Person {name: "Bob"})-[:KNOWS]->(common)
RETURN common.name

-- Friends who like the same things
MATCH (alice:Person {name: "Alice"})-[:LIKES]->(thing)<-[:LIKES]-(other)
WHERE NOT (alice)-[:KNOWS]-(other)
RETURN other.name, count(thing) as common_interests
ORDER BY common_interests DESC
Enter fullscreen mode Exit fullscreen mode

With Python

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

def get_friends(tx, name):
    result = tx.run(
        "MATCH (p:Person {name: $name})-[:KNOWS]->(friend) RETURN friend.name",
        name=name
    )
    return [record["friend.name"] for record in result]

with driver.session() as session:
    friends = session.read_transaction(get_friends, "Alice")
    print(friends)

driver.close()
Enter fullscreen mode Exit fullscreen mode

Recommendations

-- Recommend things to buy based on what similar users bought
MATCH (you:Person {name: "Alice"})-[:BOUGHT]->(item)<-[:BOUGHT]-(other)-[:BOUGHT]->(recommended)
WHERE NOT (you)-[:BOUGHT]->(recommended)
RETURN recommended.name, count(*) as score
ORDER BY score DESC
LIMIT 5
Enter fullscreen mode Exit fullscreen mode

Data Import

-- Import from CSV
LOAD CSV WITH HEADERS FROM "file:///users.csv" AS row
CREATE (:Person {
  name: row.name,
  email: row.email,
  created_at: datetime(row.created_at)
});
Enter fullscreen mode Exit fullscreen mode

This article contains affiliate links. If you sign up through the links above, I may earn a commission at no additional cost to you.

Ready to Build Your Online Business?

Get started with Systeme.io for free — All-in-one platform for building your online business with AI tools.

Top comments (0)