GraphAcademy has created a Neo4j sandbox instance for you. The graph contains a dataset of movies and people.
In this lesson, you will explore the graph and use Cypher to query it.
Nodes
The graph contains nodes labeled Movie
and Person
.
Neo4j includes a query language called Cypher that allows you to query the graph.
Run the following Cypher query to return the first 50 Movie
nodes in the graph:
MATCH (m:Movie) RETURN m LIMIT 50
You will see the Movie
nodes returned as a graph.
Click on a node to see it’s properties.
Movie
nodes have properties like title
, released
, and plot
.
Try modifying the query to return Person
nodes instead of Movie
nodes.
Click to reveal the solution
The following query would return all the Person
nodes in the graph.
The query uses Person
label to filter the nodes.
The variable p
represents the Person
nodes in the query.
MATCH (p:Person) RETURN p LIMIT 50
All nodes
You can return all the nodes by removing the LIMIT
clause.
It may take longer to return all the nodes.
MATCH (m:Movie) RETURN m
You can filter the nodes returned by adding a condition to the query.
For example, you can filter Movie
nodes by the title
property.
MATCH (m:Movie {title: 'Toy Story'}) RETURN m
Relationships
The relationships between nodes describe how people are related to movies.
For example, Person
nodes have ACTED_IN
and DIRECTED
relationships with Movie
nodes.
Run this Cypher query to return the Movie
node 'Hoffa' and Person
nodes with an ACTED_IN
relationship to the movie:
MATCH (m:Movie {title: 'Hoffa'})<-[r:ACTED_IN]-(p:Person)
RETURN m, r, p
The movie 'Hoffa' is a Movie
node with relationships to Person
nodes who acted in and directed the movie.
The query doesn’t include the DIRECTED
relationship, but it appears in the graph.
Neo4j will also display other relationships between the returned nodes to give a complete view.
Modify the query to return the movie 'Top Gun'.
Click to reveal the solution
The following query would return the movie node 'Top Gun' and the Person
nodes with an ACTED_IN
relationship to the movie.
The query uses the title
property of the Movie
node to filter the nodes.
The variable m
represents the "Top Gun" Movie
node, r
is the ACTED_IN
relationship, and p
is the Person
nodes.
MATCH (m:Movie {title: 'Top Gun'})<-[r:ACTED_IN]-(p:Person)
RETURN m, r, p
View relationships
You can double-click on nodes in the graph to expand their relationships with other nodes.
Properties
Nodes have properties that describe them.
For example, Person
nodes have properties like name
and born
, Movie
nodes have properties like title
and released
.
Relationships in Neo4j can also have properties, the ACTED_IN
relationship has a role
property, storing the person’s role in the movie.
Run the following Cypher to return the movie title
and person’s name
who acted in the movie "Top Gun":
MATCH (m:Movie {title: 'Top Gun'})<-[r:ACTED_IN]-(p:Person)
RETURN m.title, p.name
Note how the query returns the data as a table, rather than a graph, of the movie title and person’s name.
Modify the query to return the role
property of the ACTED_IN
relationship.
Click to reveal the solution
The query includes r.role
in the return.
The r
variable represents the ACTED_IN
relationship, and r.role
returns the role
property of the relationship.
MATCH (m:Movie {title: "Top Gun"})<-[r:ACTED_IN]-(p:Person)
RETURN m.title, p.name, r.role
You can learn more about Cypher in the Neo4j GraphAcademy Cypher Fundamentals course.
Check your understanding
1. Nodes in the Movies graph
What node labels are used in the Movies graph?
-
❏ ACTED_IN
-
❏ DIRECTED
-
✓ Movie
-
✓ Person
Hint
The Movie graph has two labels to represent people and movies.
Hint
The Movie graph has Person
and Movie
node labels.
Actors and directors are represented by ACTED_IN
and DIRECTED
relationships.
Summary
In this lesson, you explored a Neo4j graph containing movies and people.