It is surprisingly difficult to get counts per label, because nodes can have multiple labels, and labels (n)returns a collection of strings representing these labels. On a graph consisting of three nodes and two labels, as {:A}, {:B}and {:A:B}, labels (n)returns three different sets of rows. Instead of counting two nodes with :Aand two nodes with :B, the result will be one for each of the three combinations of labels. See console . To aggregate each label, not a collection of labels, you will have to group the values inside the collection, which is cumbersome.
I have an ugly way to do this, maybe someone can suggest a better option: first find the maximum number of labels that any node has.
MATCH (n)
RETURN max(length(labels(n)))
UNION, i , i 0 max-1. 3 ,
MATCH (n)
RETURN labels (n)[0] as name, count (n) as cnt
UNION MATCH (n)
RETURN labels (n)[1] as name, count (n) as cnt
UNION MATCH (n)
RETURN labels (n)[2] as name, count (n) as cnt
, null , . ( [0]) , . , , ,
MATCH (n)
RETURN labels (n)[0] as name, count (n) as cnt
UNION MATCH (n)
WITH labels (n)[1] as name, count (n) as cnt
WHERE name IS NOT NULL
RETURN name, cnt
UNION MATCH (n)
WITH labels (n)[2] as name, count (n) as cnt
WHERE name IS NOT NULL
RETURN name, cnt
, , , .