Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypher: loop over properties and count

Tags:

neo4j

cypher

I have a lot of nodes, containing the property gender. Possible values for gender are: male, female and andy. I am looking for a cypher query, that will return the count of each value. For example...

"male"   100
"female" 132
"andy"   12

The solution should also work for nodes with a property with more than three cases, eg. names, and the number of persons with the name.

like image 572
Thomas Avatar asked Feb 11 '23 21:02

Thomas


1 Answers

This very simple query will return a count of every distinct gender value. You can do the same thing for any property with any number of possible values.

MATCH (n)
RETURN n.gender, COUNT(*)

You should modify the MATCH clause to specifically pick nodes that actually have the property you are counting. (Any matched nodes that do not have that property would also be counted, with a value of null -- and appear in the results.)

like image 58
cybersam Avatar answered Feb 20 '23 17:02

cybersam