How to count the number of relationships in Neo4j

poorvank picture poorvank · Mar 12, 2014 · Viewed 24.7k times · Source

I am using Neo4j 2.0 and using the following query to find out the count of number of a particular relationship from a particular node.

I have to check the number of relationships named "LIVES" from a particular node PERSON.

My query is:

match (p:PERSON)-[r:LIVES]->(u:CITY) where count(r)>1  
return count(p);

The error shown is:

SyntaxException: Invalid use of aggregating function count(...)

How should I correct it?

Answer

Michael Hunger picture Michael Hunger · Mar 12, 2014

What you want is a version of having? People living in more than one city?

MATCH (p:PERSON)-[:LIVES]->(c:CITY) 
WITH p,count(c) as rels, collect(c) as cities
WHERE rels > 1
RETURN p,cities, rels