Finding more co-actors
Actors in movies that Robert Blake acted in
Given this query and what you have learned about graph traversal:
MATCH (p:Person {name: 'Robert Blake'})-[:ACTED_IN]->(m:Movie)
MATCH (allActors:Person)-[:ACTED_IN]->(m)
RETURN m.title, collect(allActors.name)
How many relationships are traversed to return the result?
-
✓ 20
Hint
The query finds each movie that Robert Blake acted in. How many ACTED_IN traversals is that?
For each movie, it uses a second MATCH
to find all actors to acted in that movie.
Because there are two MATCH
clauses, it traverses the ACTED_IN relationship from Robert Blake twice.
How many ACTED_IN relationships are traversed?
Solution
This query traverses a total of 20
ACTED_IN relationships.
-
It first traverses all ACTED_IN relationships from Robert Blake (4 traversals)
-
For each movie found, it traverses all ACTED_IN relationships to tbe movie. (16 traversals, including the traversal from Robert Blake)
You can see the nodes and relationships involved, with this query:
MATCH (p:Person {name: 'Robert Blake'})-[:ACTED_IN]->(m:Movie)
MATCH (allActors:Person)-[:ACTED_IN]->(m)
RETURN p,m,allActors
Summary
In this challenge, you determined the number of relationships that are traversed during query.
In the next lesson, you will learn about traversal with variable-length paths in the graph.