0
votes

I am new to Neo4j and going through their introduction tutorial and I am a little bit confused about the use of the 'WHERE NOT exists' clause. The tutorial shows an example where they want to return all Person nodes of people who wrote movies and exclude people who directed that movie. Their query for this is:

MATCH (p:Person)-[:WROTE]->(m:Movie) WHERE NOT exists( (p)-[:DIRECTED]->() ) RETURN p.name, m.title

which returns:

p.name m.title "Aaron Sorkin" "A Few Good Men" "Jim Cash" "Top Gun" "David Mitchell" "Cloud Atlas"

My query for this is:

MATCH (p:Person)-[:WROTE]->(m:Movie) WHERE NOT exists( (p)-[:DIRECTED]->(m) ) RETURN p.name, m.title

which returns:

p.name m.title "Aaron Sorkin" "A Few Good Men" "Jim Cash" "Top Gun" "Nora Ephron" "When Harry Met Sally" "David Mitchell" "Cloud Atlas" "Lana Wachowski" "V for Vendetta" "Lilly Wachowski" "V for Vendetta"

I checked the graph relationships for "Nora Ephron", "Lana Wachowski" and "Lilly Wachowski" (who are not in their query result) and found that they wrote and produced the movies (but not directed). According to my understanding both of the queries should return the same result. I am wondering why they are returning different results. Any help would be appreciated for explaining the differences.

1

1 Answers

1
votes

These queries are different from each other, they are asking for different things and returning different results.

MATCH (p:Person)-[:WROTE]->(m:Movie)
 WHERE NOT exists( (p)-[:DIRECTED]->() )
 RETURN p.name, m.title

The WHERE clause in the above query ensures that the p person has not directed anything (they have no :DIRECTED relationships from the p node).

MATCH (p:Person)-[:WROTE]->(m:Movie)
 WHERE NOT exists( (p)-[:DIRECTED]->(m) )
 RETURN p.name, m.title

The WHERE clause in the above query is different because of the presence of m in the pattern, which refers to the same movie as in the MATCH. This query ensures that the movie being considered for this person was written by p, but not directed by p. p may have directed some other movie, and maybe they both wrote and directed some other movie, but for this movie m, they only wrote it, they did not direct it.