Neo4j and Cypher: Finding Directors who Acted in Their Own Movie
Join the DZone community and get the full member experience.
Join For FreeI’ve been doing quite a few Intro to Neo4j sessions recently and since it contains a lot of problems for the attendees to work on I get to see how first time users of Cypher actually use it.
A couple of hours in we want to write a query to find directors who acted in their own film based on the following model.

A common answer is the following:
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d) WHERE a.name = d.name RETURN a
We’re matching an actor ‘a’, finding the movie they acted in and then finding the director of that movie. We now have pairs of actors and directors which we filter down by comparing their ‘name’ property.
I haven’t written SQL for a while but if my memory serves me correctly comparing properties or attributes in this way is quite a common way to test for equality.
In a graph we don’t need to compare properties – what we actually want to check is if ‘a’ and ‘d’ are the same node:
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d) WHERE a = d RETURN a
We’ve simplifed the query a bit but we can actually go one better by binding the director to the same identifier as the actor like so:
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(a) RETURN a
So now we’re matching an actor ‘a’, finding the movie they acted in and then finding the director if they happen to be the same person as ‘a’.
The code is now much simpler and more revealing of its intent too.
Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Managing Data Residency, the Demo
-
Why You Should Consider Using React Router V6: An Overview of Changes
-
What Is React? A Complete Guide
-
Writing a Vector Database in a Week in Rust
Comments