Get Record Count in Neo4j Graph Database with example

Get Record Count in Neo4j Graph Database

Today in this article, we will see a sample example to Get Record Count in Neo4j Graph Database.

Updated October 2026: a section on verifying the count store with EXPLAIN, a section on relationship and COUNT subquery pitfalls, and an FAQ.

Today in this article, we will cover below aspects,

Please note that the Neo4j node Database maintains a transactional count store for holding count metadata. Due to some reason when you query the count using aggregation functions like count() , simple, unfiltered counts can be answered immediately from the count store.

Let’s take the example below. I have below 2 nodes with the names Movie and Person.

Get the total count from all Nodes

Lets the total count of records available in all nodes

Query

MATCH (n) RETURN count(n) as count

OR

MATCH () RETURN count(*) as count

Above both queries will return the same result.

Example

Get Record Count in Neo4j Graph Database

Get the total count from specific Nodes

Let’s now get the count of records from a specific node like Movie or Person

Que

Query

MATCH (n:Person)
RETURN count(n) as count

OR

MATCH (:Person)
RETURN count(*) as count

Above both queries will return the same result.

Example

how-to-count-the-numbers-of-node-types-in-the-neo4j-graph

Get the Total Count from specific Nodes using Relationship 

As we discuss above Neo4j node Database maintains a transactional count store for holding count metadata. This also covers simple relationship patterns such as ()-[:TYPE]->(), but not patterns with labels on both ends.

Query

MATCH ()-[r:ACTED_IN]->(:Movie) RETURN count(r) as count

OR

MATCH (:Person)-[r:ACTED_IN]->() RETURN count(r) as count

Example

how-count-node-cypher-neo4j

Useful References :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.



How to check that a Neo4j count query uses the count store

Put EXPLAIN in front of the count query and read the plan. If it contains a NodeCountFromCountStore or RelationshipCountFromCountStore operator, Neo4j is answering from stored metadata. EXPLAIN shows the plan without executing the query, so it’s safe to run against a large database.

This matters because a count-store lookup reads a number Neo4j already maintains. It doesn’t scan nodes or relationships, so it stays fast as the database grows. Any other plan has to touch the data, and its cost grows with it.

In plain English: It’s the difference between reading “12,000 books” off the library’s catalogue card and walking the shelves to count them. The analogy stops fitting in one way: the card only exists for a few simple questions, so a slightly different question sends you back to the shelves.

Which count queries qualify?

These forms can be answered from the count store:

  • All nodes: MATCH (n) RETURN count(n)
  • One label: MATCH (n:Person) RETURN count(n)
  • Variable-free forms: MATCH () RETURN count(*) and MATCH (:Person) RETURN count(*)

What stops Neo4j from using the count store?

Any of the following forces a normal plan:

  • A WHERE clause: the store holds totals, not filtered subsets.
  • Inline properties: a pattern like (:Person {name: 'Tom'}) is a filter in disguise.
  • Another variable in scope: if something else sits next to count(), the store is skipped.
  • A count that isn’t alone: count() must be the only item in the RETURN or WITH.

Which correct counts still aren’t count-store lookups?

Three common cases return the right number but run a real query plan:

  • Labels on both ends: (:Person)-[:WROTE]->(:Movie) is counted correctly, but the store tracks a label on only one end of a relationship. Neo4j does a label scan, expands, and filters.
  • Grouped counts: MATCH (n) RETURN labels(n), count(n) is handy for profiling the data, but it isn’t a count-store lookup.
  • Filtered counts: the count store can’t help here at all.

For filtered counts, the practical lever is an index on the property you filter by. It lets Neo4j find the matching nodes without scanning every node with that label. The count still takes work proportional to the matches, so it won’t be instant when millions of nodes match.

Frequently asked questions

Is MATCH (n) RETURN count(n) slow on a large Neo4j database?

Usually not. A bare count of all nodes or of one label can be answered from the transactional count store without scanning anything. Add a WHERE clause or inline properties and it becomes a real scan. Run EXPLAIN on the query and look for a NodeCountFromCountStore operator to confirm.

Why is my Neo4j count query not using the count store?

Something in the query is stopping the planner from using it. Common causes are a WHERE clause, inline properties in the pattern, another variable in scope beside count(), or a relationship pattern with labels on both ends. Grouped counts such as labels(n) with count(n) don’t use it either. Simplify the query to a bare count() and re-run EXPLAIN to check.

What is the difference between count(*), count(n) and count(n.property) in Cypher?

count(*) counts rows, including rows that contain null values. count(n) counts the non-null values of n. count(n.property) skips nodes where the property is null, so it counts only the nodes that have it. count(null) returns 0.

Why does my Neo4j relationship count come out double?

Your pattern is probably undirected. A pattern such as (n)-[r]-() matches each directed relationship from both ends, so it counts every relationship twice. Write the direction explicitly, for example ()-[r:TYPE]->(), to get the true total.

Leave a Reply

Your email address will not be published. Required fields are marked *