cancel
Showing results forΒ
Did you mean:Β

## In Exercise 4.11, why directors are not listed in the actor column?

Node

Hi All,

Either with the tutorial command

MATCH (a1:Person)-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(a2:Person)
WHERE exists( (a2)-[:DIRECTED]->(m) )
RETURN a1.name as Actor, a2.name as Actor/Director, m.title as Movie

or with this command

match (p:Person) -[:ACTED_IN]-> (m:Movie) <-[:DIRECTED]- (p), (ac:Person) -[:ACTED_IN]-> (m)
return ac.name as actor, p.name as director, m.title as movie

Even if we know that directors are also actors, they are not listed in the actor column.
Can someone explain me why?

Regards,
Patrick

Neo4j

Sure.

So in the first query, the requirement is we're finding the entire pattern, and one of the requirements of this pattern is that whoever is matched to a2 has to have also directed the movie.

The movies in question ("That Thing You Do", "Unforgiven", and "Hoffa") only have a single director each.

So while we could possibly have a1 match to the actor/director in all these cases, there is no other (a2) person who directed the movie, so the pattern cannot be fulfilled by binding the directors to a1.

Now if there was a second director on each of these movies, and that second director also happened to act in the movie, then we could successfully match on that pattern.

Note also that relationships must be unique within matched patterns, which is why the same person from a1 cannot also be matched to a2, since it would require us to traverse the single :ACTED_IN relationship between them and the movie twice, which isn't allowed.

That is also why in the second query whoever we match to p cannot be bound to ac as well, because the single :ACTED_IN relationship between the person and the movie may only be traversed once per path, and it was already used in the first pattern, so it cannot be used in the second part of the pattern. This has the consequence that p and ac must be separate people.

Nodes 2022

NODES 2022, Neo4j Online Education Summit

OnΒ November 16 and 17 for 24 hours across all timezones, youβll learn about best practices for beginners and experts alike.

Neo4j Resources