Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j - Counting Nodes with Labels

Tags:

neo4j

I'd like a query that counts how many nodes have each label in the dataset. For instance:

LabelA 100 LabelB 200

I can do this for each individual label with something like

MATCH (n:LabelA) return count(n);

But, I'd like to do it for every label in one command.

like image 685
leadZERO Avatar asked Feb 04 '14 14:02

leadZERO


People also ask

How many nodes are in Neo4j?

The limitation is currently 2^35, so approximately 34 billion nodes.

How many nodes can a single relationship connect in Neo4j?

Relationships provide directed, named semantic connections between two nodes. A relationship always has a direction, a type, a start node, and an end node.

What is unwind in Neo4j?

With UNWIND , you can transform any list back into individual rows. These lists can be parameters that were passed in, previously collect -ed result or other list expressions. One common usage of unwind is to create distinct lists. Another is to create data from parameter lists that are provided to the query.


2 Answers

Try something like this

MATCH (n) 
RETURN DISTINCT count(labels(n)), labels(n);

This will return the sum of the labels in the first column and the label name in the second.

like image 119
tehAnswer Avatar answered Oct 04 '22 00:10

tehAnswer


A quick alternative here, for single labels only, APOC Procedures offers a quick means of using the counts store to get the counts:

CALL apoc.meta.stats() YIELD labels
RETURN labels
like image 37
InverseFalcon Avatar answered Oct 04 '22 00:10

InverseFalcon