Find neo4j nodes with more than one incoming relationship

SteveS picture SteveS · Apr 10, 2014 · Viewed 14.6k times · Source

I'm trying to find all the nodes with more than one incoming relationship. Given this data:

a-[has]->b
a-[has]->c
d-[has]->b

So, I'm looking for a query that returns 'b', because it has more that one incoming.

This query is close. It returns 'a' and 'b', because they both have 2 relations:

match (n)--()
with n,count(*) as rel_cnt
where rel_cnt > 1
return n;

However, this query (the addition of '-->') doesn't return any and I don't know why:

match (n)-->()
with n,count(*) as rel_cnt
where rel_cnt > 1
return n;

Am I going about this all wrong?

Answer

cybersam picture cybersam · Apr 10, 2014

Does this work for you?

MATCH ()-[r:has]->(n)
WITH n, count(r) as rel_cnt
WHERE rel_cnt > 1
RETURN n;

I am assuming, perhaps incorrectly, that 'has' is the appropriate relationship type. If not, then try:

MATCH ()-[r]->(n)
WITH n, count(r) as rel_cnt
WHERE rel_cnt > 1
RETURN n;