0
votes

I'm doing some codes on the Neo4j's movies dataset the question was

Retrieve the actors who have acted in exactly five movies, returning the name of the actor, and the list of movies for that actor.

I wrote this following query and im not getting the result and it shows "no changes no result"

MATCH (a:Person)-[:ACTED_IN]->(m:Movie)
WITH  a,m, count(m) AS numMovies
WHERE numMovies = 5
RETURN a.name,collect(m.title) AS movies

where as when I wrote this query for the same satement this time I just write the "collect(m.title) AS movies " in the WITH clause and I got the desired result.

MATCH (a:Person)-[:ACTED_IN]->(m:Movie)
WITH  a, count(m) AS numMovies, collect(m.title) AS movies
WHERE numMovies = 5
RETURN a.name, movies

My doubt is that why result varies when I wrote the "collect(m.title) AS movies" in the RETURN clause.

1

1 Answers

0
votes

Your first query has m, count(m), which will result in a count of 1 for each Movie node m.

You can check this by returning from the query in the second line:

MATCH (a:Person)-[:ACTED_IN]->(m:Movie)
RETURN a, m, count(m) AS numMovies

The solution is to remove the separate m variable from the WITH clause as shown in your second query.