Reputation: 43
I've been trying to filter all nodes that are not connected to nodes of a given type, and discovered an odd behavior.
Specifically in my current small example, I had two actors connected to a single movie, and another movie with nothing connected to it.
This query worked fine:
MATCH (a:Actor) WHERE NOT (a)-->(:Movie) RETURN a
It returned no actors, as both actors starred in my one movie.
However, when I wrote it like this
MATCH (a:Actor),(m:Movie) WHERE NOT (a)-->(m) RETURN a
it returned both actors.
In reverse, the query
MATCH (m:Movie) WHERE NOT (m)<--(:Actor) RETURN m
worked as expected, returning the movie nobody starred in, but this time,
MATCH (m:Movie),(a:Actor) WHERE NOT (m)<--(a) RETURN m
also returned only the movie nobody starred in! What was odd, though, is that it returned 2 rows, both of them being the movie nobody starred in.
All in all, I'm completely confused.
Upvotes: 4
Views: 70
Reputation: 11216
The simple answer is you are generating a cartesian product when you are asking for all movies and all actors. When you filter out actors that aren't in the movie that works for one movie but not for the second movie where both actors did not act.
MATCH (a:Actor),(m:Movie) WHERE NOT (a)-->(m) RETURN a
What was your express goal with the second query?
If your data set is really 4 nodes small then try these queries in succession and I think you will see what is happening.
Full cartesian product
MATCH (a:Actor),(m:Movie) RETURN m,a
With the where clause added
MATCH (a:Actor),(m:Movie) WHERE NOT (a)-->(m) RETURN m,a
Upvotes: 2