When working with highly connected data, graph databases are ultra fast and much easier to query than relational databases. You can query key value pairs in a graph database and also query the relationships between entities. Querying relationships is a powerful tool because it helps you to find patterns in data and discover new insights.
Neo4j is the world’s leading graph database. It’s scalable, versatile and user-friendly. I used a Neo4j Aura workspace on my browser to create a graph representing a design team.
To recreate the graph in the design team.png file, run the following query against an empty Neo4j database:
CREATE
(angele:Employee {name: 'Angele Kesta', salary:70000, location: 'Vilnius'}),
(mantas:Employee {name: 'Mantas Pasta', salary:55000, location: 'Vilnius'}),
(ronnie:Employee {name: 'Ronnie Scott', salary:110000, location: 'Tel Aviv'}),
(justina:Employee {name:'Justina Detail', salary: 60000, location: 'Vilnius'}),
(george:Employee {name: 'George Dzinga', salary: 80000, location: 'Amsterdam'}),
(sally:Intern {university: 'Vilnius Technical University', name: 'Sally Jones', location: 'Vilnius'}),
(events:Project {title: 'Events Seating Map'}),
(angele)-[:DESIGNED {role: 'Lead Designer'}]->(events),
(mantas)-[:DESIGNED {role: 'Junior Designer'}]->(events),
(ronnie)-[:PROJECT_MANAGED {role: 'Head of Project'}]->(events),
(justina)-[:EDITED]->(events),
(george)-[:EDITED]->(events),
(forums:Project {title: 'Forum Comments'}),
(george)-[:DESIGNED {role: 'UX Writer'}]->(forums),
(mantas)-[:DESIGNED {role: 'Junior Designer'}]->(forums),
(ronnie)-[:MENTORED]->(angele),
(ronnie)-[:MENTORED]->(mantas),
(george)-[:MENTORED]->(mantas),
(mantas)-[:MENTORED]->(sally)This query is written in Cypher code which is Neo4j's graph query language, used to add nodes and properties to a graph, and to retrieve data.
Nodes are used to represent real-world objects, such as people or products. In the design team graph, nodes represent employees, projects and an intern.
-
There are six nodes representing people in the design team. Five of these nodes have the label
Employeeand include properties about the employee’sname,salaryandlocation. One of these nodes has the labelInternand includes properties about the intern’suniversity,nameandlocation. -
There are two project nodes with the label
Project. The project titles areEvents Seating MapandForum Comments.
The graph demonstrates the roles employees had in the projects and how employees interact with each other. This information is represented as relationships between nodes. A relationship describes a connection between a source node and a target node.
Let’s discuss Ronnie’s relationships in the graph to illustrate how relationships are structured in Neo4j and demonstrate the power of graph databases:
-
Cypher syntax is used to implement the mentor relationship between
Ronnie ScottandAngele Kesta. -
Cypher provides a visual way of demonstrating relationships, using a syntax similar to ASCII-art where
(nodes)-[:ARE_CONNECTED_TO]→(otherNodes)using round brackets for circular(nodes), and-[:ARROWS]→to represent relationships. -
The Cypher syntax representing the design team graph shows that
(ronnie)-[:MENTORED]→(angele). -
Ronnie Scott also mentored Mantas Pasta.
-
Ronnie Scott was also Head of Project while designing the Events Seating Map. Therefore the relationship between Ronnie and the Events Seating Map is labeled
PROJECT_MANAGEDand includes a propertyrolewith the value Head of Project.
|
Note
|
There are additional relationships in the graph, as the employees mentored each other, and one employee mentored Sally the intern. The employees also have relationships with the projects. Refer to the design team.png file to see which employees designed or edited the projects and their exact roles. You will see all the properties of the nodes, and the properties of the relationships. |
If I had a larger graph modelling an entire organisation, a fantastic route for exploration would be to see who has impacted the most employees in terms of mentoring (direct mentees, mentees of direct mentees etc.) and how the projects benefitted from these relationships.
Cypher can be used to run queries to get answers about nodes and their relationships. This section provides example queries and information retrieved from the graph.
MATCH (mentor:Employee|Intern)-[:MENTORED]->(mentee:Employee|Intern)
RETURN mentor.name, COUNT(*) AS num_mentees
ORDER BY num_mentees DESCThis query returns the name of the employee or intern who has the most mentees first, in this case it’s Ronnie Scott, then orders the rest of the results in descending order.
| mentor.name | num.mentees |
|---|---|
|
|
|
|
|
|
|
|
MATCH (ronnie:Employee {name: 'Ronnie Scott'})-[:MENTORED]->(mentees)-[:MENTORED]->(mentees_of_mentees)
RETURN mentees_of_mentees.nameRonnie Scott’s indirect mentee is Sally Jones. This query works by first matching Ronnie Scott, his direct mentees, then all of the mentees of his direct mentees.
| mentees_of_mentees.name |
|---|
|
|
MATCH(e:Employee {salary:70000})
RETURN eReturns the employee who earns a salary of 70000 and the properties.
| e |
|---|
|
|
WITH 60000 AS minSalary
MATCH (a:Employee WHERE a.name = 'Ronnie Scott')-[:MENTORED]->(b:Employee WHERE b.salary > minSalary)
RETURN b.nameReturns the employees who were mentored by Ronnie Scott and have a salary greater than 60000.
| b.name |
|---|
|
|
MATCH (angele {name: 'Angele Kesta'})-[:DESIGNED]->(project)<-[:EDITED]-(editor)
RETURN project.title, editor.nameReturns the project Angele Kesta designed and its editors.
| project.title | editor.name |
|---|---|
|
|
|
|
|
|
MATCH (events {title: 'Events Seating Map'})<-[:DESIGNED|EDITED]-(employee)
RETURN employee.nameReturns nodes with a DESIGNED or EDITED relationship towards the project node Events Seating Map.
| employee.name |
|---|
|
|
|
|
|