How to query nodes with node degree greater than 1


(Namestudent) #1

When I want to inquire about the names and number of actors who have appeared in two movies, I can write this way.
match (actor:Person) -[acted_in] ->(movie:Movie)
with actor,count(movie) as num_m
where num_m>2
return actor {.name,num_m}

But when I wanted to check out his films

match (actor:Person) -[acted_in] ->(movie:Movie)
with actor,count(movie) as num_m , movie
where num_m>2
return actor {.name,num_m},movie{.title}

But the result of my inquiry is incorrect. I don't know where I made a mistake, so I asked for help.

Thank you

(Stefan Armbruster) #2

The crucial thing to understand here is how aggregations work: If you have an aggregation function (e.g. count) in a WITH (or RETURN) the base for the aggregation are all non-aggregated expression in that part - in your example actor, movie. Therefore it's obvious that the count is always 1 since we don't have actor acting in the same movie twice.

To fix your statement I see to options:

MATCH (actor:Person) 
WHERE size((actor)-[:ACTED_IN]->() ) > 1
MATCH (actor)-[:ACTED_IN]->(movie:Movie)
RETURN actor {.name,num_m},movie{.title}


MATCH (actor)-[:ACTED_IN]->(m:Movie)
WITH actor, count(movie) as c, collect(m) as movies
UNWIND movies as movie
RETURN actor {.name,num_m},movie{.title}

(Namestudent) #3

excellent!!that‘s right ,thank you very much