Neo4j Practical Implementation
Scenario: Online Course Management System
We are developing a course management system using Neo4j. The system will contain
nodes representing Students, Courses, and Professors. The relationships between these
entities will be as follows:
- Student attends Course.
- Professor teaches Course.
- Student is guided by a Professor.
This system will support basic CRUD (Create, Read, Update, Delete) operations. We'll also
use Neo4j clauses such as MERGE, WRITE, and READ and apply string and mathematical
functions. Lastly, we will create user-defined functions to extend Neo4j's capabilities.
Step 1: Creating Nodes in Neo4j
We start by creating nodes for Students, Courses, and Professors:
CREATE (s1:Student {name: 'John', age: 21, student_id: 'S101'})
CREATE (s2:Student {name: 'Alice', age: 22, student_id: 'S102'})
CREATE (c1:Course {name: 'Data Science', course_id: 'C101', credits: 4})
CREATE (c2:Course {name: 'AI and Machine Learning', course_id: 'C102', credits: 5})
CREATE (p1:Professor {name: 'Dr. Smith', age: 45, professor_id: 'P101'})
CREATE (p2:Professor {name: 'Dr. Jane', age: 39, professor_id: 'P102'})
Step 2: Creating Relationships Between Nodes
Next, we will establish relationships between the nodes using the CREATE clause:
- John attends Data Science
- Alice attends AI and Machine Learning
- Dr. Smith teaches Data Science
- Dr. Jane teaches AI and Machine Learning
- John is guided by Dr. Smith
- Alice is guided by Dr. Jane
CREATE (s1)-[:ATTENDS]->(c1)
CREATE (s2)-[:ATTENDS]->(c2)
CREATE (p1)-[:TEACHES]->(c1)
CREATE (p2)-[:TEACHES]->(c2)
CREATE (s1)-[:GUIDED_BY]->(p1)
CREATE (s2)-[:GUIDED_BY]->(p2)
Step 3: Using the MERGE Clause for Preventing Duplicates
If you want to ensure that no duplicate nodes are created, you can use the MERGE clause:
MERGE (s3:Student {name: 'John', student_id: 'S101'})
In this case, if the Student node with the same student_id already exists, no new node will be
created.
Step 4: Writing Queries with WRITE and READ Clauses
Write Example: Adding a new student and course:
WRITE {
CREATE (s3:Student {name: 'Charlie', age: 23, student_id: 'S103'})
CREATE (c3:Course {name: 'Advanced Mathematics', course_id: 'C103', credits: 3})
}
Read Example: Fetching all students:
READ {
MATCH (s:Student) RETURN s.name, s.age, s.student_id
}
Step 5: Applying String and Mathematical Functions
String Function Example: Retrieve student names in uppercase:
MATCH (s:Student)
RETURN s.name, toUpper(s.name) AS name_uppercase
Mathematical Function Example: Increase the credits of each course by 1:
MATCH (c:Course)
SET c.credits = c.credits + 1
RETURN c.name, c.credits
Step 6: Creating User-Defined Functions
In Neo4j, we can define our own functions. Let's create a simple function to calculate the
total credits for a student:
CALL apoc.custom.asFunction(
'studentTotalCredits',
'MATCH (s:Student)-[:ATTENDS]->(c:Course) WHERE s.student_id = $student_id RETURN
SUM(c.credits) AS total_credits',
'LONG',
['student_id', 'STRING']
);
Use the custom function to calculate the total credits for a student:
RETURN apoc.custom.studentTotalCredits('S101') AS totalCredits
Step 7: Performing CRUD Operations
1. Create Operation: Add a new student:
CREATE (s4:Student {name: 'David', age: 24, student_id: 'S104'})
2. Read Operation: Retrieve all courses along with the professors teaching them:
MATCH (p:Professor)-[:TEACHES]->(c:Course)
RETURN p.name AS Professor, c.name AS Course
3. Update Operation: Update a student’s age:
MATCH (s:Student {student_id: 'S101'})
SET s.age = 22
RETURN s
4. Delete Operation: Delete a course:
MATCH (c:Course {course_id: 'C103'})
DETACH DELETE c