The query you provided does not return a list of movies. It returns a stream of records (or rows) of titles of movies that matched the pattern.
Unless you explicitly collect()
something, or you use pattern comprehensions, or you otherwise invoke a function or procedure that results in a list, then you're not working with lists.
Let's go back to the queries in your original question.
First, a reference query whose results we may refer to later: Let's get all coactors of Tom Hanks:
match (p:Person{name:'Tom Hanks'})-[:ACTED_IN*2]-(coactor)
return distinct coactor.name
Again, this isn't a list...it's a stream of rows. Each row is an individual result, the coactor
that was matched to in the pattern.
If you look at the results, you'll find that Charlize Theron is a coactor of Tom Hanks (if you perform some additional queries you would see that they co-acted in "That Thing You Do").
Now let's only take the first part of those queries, the MATCH but not the WHERE, and return the contact and the movie:
match (p:Person{name:'Tom Hanks'}) - [:HAS_CONTACT*2] -> (x:Person),
(x) - [:ACTED_IN] -> (m)
return x.name, m.title
Again, there are no lists here. The return is a stream of rows, where each row refers to a separate person and movie node that matched the pattern.
Note some of the matches that may be near the top:
╒════════════════════════╤═════════════════════════════════╕
│"x.name" │"m.title" │
╞════════════════════════╪═════════════════════════════════╡
│"Charlize Theron" │"That Thing You Do" │
├────────────────────────┼─────────────────────────────────┤
│"Charlize Theron" │"The Devil's Advocate" │
├────────────────────────┼─────────────────────────────────┤
Each line corresponds with a path found in the graph that fits the pattern, where each node variable refers to an individual node, not a collection.
Looking at JUST these two rows, we can see that indeed Charlize Theron is someone Tom Hanks has had contact with (2 :HAS_CONTACT hops). Charlize has acted in movies. Since this graph only has two movies she has acted in, both were matched.
So in the first row, x
matched to Charlize Theron, and m
, a movie she acted in, was matched to That Thing You Do. In the next row, we still have Charlize Theron for x
, and m
in this case is the other movie she acted in, The Devil's Advocate.
If we applied the relevant part of the WHERE clause in the first query here:
where not (p) - [:ACTED_IN] -> () <- [:ACTED_IN] - (x)
this would eliminate Charlize Theron, because this pattern exists in the graph, Tom Hanks has acted in something that Charlize Theron has acted in (as mentioned above...they are coactors through That Thing You Do). Both rows with Charlize Theron would be filtered out. The key thing to see is that m
has nothing to do with that predicate (excluding anyone that has acted in something Tom Hanks has acted in).
However, if we only used the WHERE clause of the second query:
where not (p) - [:ACTED_IN] -> (m)
then only one row would be filtered out: the row with Charlize Theron and That Thing You Do, since Tom Hanks acted in That Thing You Do (but he didn't act in The Devil's Advocate). This predicate deals with m
specifically. For the movie node matched by m
, we're excluding the row if Tom Hanks acted in that movie.
Charlize Theron would remain, because Charlize Theron acted in a movie that Tom Hanks did not act in.