👋 Introduction
Ever wondered how Netflix or Marvel fans get “because you watched this…” suggestions? In this blog, I’ll walk you through building a real-world Movie Recommendation System using Neo4j (graph database) and Node.js.
Unlike traditional SQL databases, Neo4j shines when you want to store relationships (like “User → Likes → Movie”). That’s why it’s perfect for recommendations.
We’ll build a mini Netflix-style engine that knows which movies you like and suggests others you might enjoy.
🛠️ Tech Stack
Neo4j → Graph database to store movies, users, and relationships.
Node.js → Server-side runtime to interact with Neo4j.
neo4j-driver → Official driver to connect Node.js with Neo4j.
Github Repository:-https://github.com/Kshitij-0007/neo4j
📂 Project Setup
1. Initialize Project
mkdir neo4j-movie-recommendation
cd neo4j-movie-recommendation
npm init -y
npm install neo4j-driver dotenv
- Add .env File
NEO4J_URI=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=yourpassword
- package.json Update
{
"name": "movie-recommendation",
"version": "1.0.0",
"description": "A movie recommendation system using Neo4j and Node.js",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"neo4j-driver": "^5.15.0",
"dotenv": "^17.2.1"
}
}
🎥 Sample Data (Marvel + Netflix Movies)
We’ll create some users and movies, then connect them with LIKES relationships.
index.js
import neo4j from "neo4j-driver";
import dotenv from "dotenv";
dotenv.config();
const driver = neo4j.driver(
process.env.NEO4J_URI,
neo4j.auth.basic(process.env.NEO4J_USER, process.env.NEO4J_PASSWORD)
);
const session = driver.session();
async function main() {
try {
// Clear existing DB
await session.run("MATCH (n) DETACH DELETE n");
// Create Movies
await session.run(`
CREATE (m1:Movie {title: "Avengers: Endgame", genre: "Action"})
CREATE (m2:Movie {title: "Doctor Strange", genre: "Fantasy"})
CREATE (m3:Movie {title: "The Witcher", genre: "Fantasy"})
CREATE (m4:Movie {title: "Money Heist", genre: "Thriller"})
CREATE (m5:Movie {title: "Stranger Things", genre: "Sci-Fi"})
CREATE (m6:Movie {title: "Loki", genre: "Action"})
`);
// Create Users
await session.run(`
CREATE (u1:User {name: "Arjun"})
CREATE (u2:User {name: "Meera"})
CREATE (u3:User {name: "Karan"})
`);
// Create Relationships
await session.run(`
MATCH (u1:User {name: "Arjun"}), (m1:Movie {title: "Avengers: Endgame"})
CREATE (u1)-[:LIKES]->(m1);
MATCH (u1:User {name: "Arjun"}), (m2:Movie {title: "Doctor Strange"})
CREATE (u1)-[:LIKES]->(m2);
MATCH (u2:User {name: "Meera"}), (m3:Movie {title: "The Witcher"})
CREATE (u2)-[:LIKES]->(m3);
MATCH (u2:User {name: "Meera"}), (m4:Movie {title: "Money Heist"})
CREATE (u2)-[:LIKES]->(m4);
MATCH (u3:User {name: "Karan"}), (m5:Movie {title: "Stranger Things"})
CREATE (u3)-[:LIKES]->(m5);
MATCH (u3:User {name: "Karan"}), (m6:Movie {title: "Loki"})
CREATE (u3)-[:LIKES]->(m6);
`);
console.log("✅ Database seeded with movies and users!");
// Recommendation Query
const result = await session.run(`
MATCH (u:User {name: "Arjun"})-[:LIKES]->(m:Movie)<-[:LIKES]-(other:User)-[:LIKES]->(rec:Movie)
WHERE u <> other AND NOT (u)-[:LIKES]->(rec)
RETURN DISTINCT rec.title AS recommendation
`);
console.log("🎯 Recommendations for Arjun:");
result.records.forEach(record => {
console.log("👉 " + record.get("recommendation"));
});
} catch (error) {
console.error("Error:", error);
} finally {
await session.close();
await driver.close();
}
}
main();
🚀 Run the Project
npm start
You’ll see something like:
✅ Database seeded with movies and users!
🎯 Recommendations for Arjun:
👉 The Witcher
👉 Money Heist
👉 Stranger Things
👉 Loki
🧩 How It Works
Users are connected to movies via LIKES relationships.
The Cypher query finds other users with similar tastes.
It returns movies they liked that Arjun hasn’t watched yet.
This mimics a collaborative filtering recommendation system.
📊 Visualizing the Graph
Open the Neo4j Browser (http://localhost:7474/) and run:
MATCH (n) RETURN n
You’ll see users and movies connected in a graph — like your own mini Netflix brain 🧠.
🎁 Next Steps
Add more genres and ratings.
Build a simple frontend to display recommendations.
Expand dataset with real movie datasets (IMDb, TMDb).
🙌 Conclusion
And there you have it — a Movie Recommendation System in less than 100 lines of code using Neo4j + Node.js.
Graph databases make relationships first-class citizens, which makes recommendation engines super easy and natural.
If you enjoyed this, drop a ❤️ on Dev.to, connect with me, and check out the GitHub repo.
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.