Showing results for 
Search instead for 
Did you mean: 

How to query nodes with node degree greater than 1

Node Clone

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


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}

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

Nodes 2022
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.