0
votes

Not sure if the title explains everything but I'm learning Cypher using Neo4j and the Movie Database (from here: https://neo4j.com/developer/example-data/) and I'm trying to craft a query that will give me every movie whose director also acted in it.

To give an example of such a movie, Pulp Fiction - Quentin Tarantino both acted in and directed the movie.

I came up with this query for the above example:

match (m:Movie)-[:ACTS_IN]-(d:Director)-[:DIRECTED]->(n:Movie)
where d.name STARTS WITH 'Q'
return *

But it doesn't seem to work the way I expected it to. I get all the movies he's directed and acted in, whereas I only want the ones where he's done both.

2

2 Answers

1
votes

You already had d and m declared, so you don't assign labels in the second MATCH statement again. The correct query would look like this

MATCH (d:Director)-[:DIRECTED]->(m:Movie) 
WHERE d.name STARTS WITH 'Q'     
MATCH (d)-[:ACTS_IN]->(m) 
RETURN d, collect(m) AS movies

Note that based on your initial question, I've modified the return statement as well.

0
votes

Oh wow I just figured it out:

match (d:Director)-[:DIRECTED]->(m:Movie)
match (d:Director)-[:ACTS_IN]->(m:Movie)
where d.name STARTS WITH 'Q' 
return *

I was honestly expecting to have to use some kind of operator to join the two, but I guess I was wrong.