Count relationships in Neo4j
To count a node’s relationships, use a COUNT { … } subquery on the pattern. It returns 0 for
nodes with none, and it does not change the number of rows:
MATCH (u:User)
RETURN u.email AS email, COUNT { (u)-[:AUTHORED]->(:Post) } AS posts
ORDER BY posts DESCWith Alice (3 posts), Bob (1) and Carol (none), this returns 3, 1 and 0. COUNT { }
subqueries need Neo4j 5.3 or later.
The classic form: OPTIONAL MATCH and count()
MATCH (u:User)
OPTIONAL MATCH (u)-[:AUTHORED]->(p:Post)
RETURN u.email AS email, count(p) AS postsSame result. count() is an aggregation: every column that is not aggregated (email here)
becomes a grouping key, as GROUP BY would be in SQL.
Two easy mistakes with this form. count(*) counts rows, and OPTIONAL MATCH produces one row
(with p = null) for a user without posts, so Carol gets 1 instead of 0: count the variable,
count(p), which skips nulls. And with a plain MATCH instead of OPTIONAL MATCH, users with no
posts disappear from the result entirely.
Filter on a count
MATCH (u:User)
WHERE COUNT { (u)<-[:FOLLOWS]-(:User) } >= 2
RETURN u.email AS emailDegree: all relationships of a node
Leave out the type and direction to count everything attached to a node, or split it up:
MATCH (u:User {email: $email})
RETURN COUNT { (u)--() } AS degree,
COUNT { (u)<-[:FOLLOWS]-() } AS followers,
COUNT { (u)-[:FOLLOWS]->() } AS followingCount every relationship of a type, or by type
MATCH ()-[f:FOLLOWS]->()
RETURN count(f) AS followsMATCH ()-[r]->()
RETURN type(r) AS type, count(*) AS total
ORDER BY total DESCUse a directed pattern (->) here: an undirected ()-[r]-() matches each relationship twice, once
from each end.
Several aggregates in one row
MATCH (u:User)-[:AUTHORED]->(p:Post)
RETURN u.email AS email,
count(p) AS posts,
count(CASE WHEN p.published THEN 1 END) AS published,
collect(p.title) AS titlescount(DISTINCT x) counts distinct values, for example how many different users are followed by
anyone: MATCH (:User)-[:FOLLOWS]->(followed:User) RETURN count(DISTINCT followed).
In Cyphra
count counts nodes of one model, with the same where filters as findMany, including relation
filters:
import { db } from "./db.js";
await db.user.count(); // all users
await db.user.count({ where: { posts: { some: { published: true } } } }); // users with a published post
await db.user.count({ where: { posts: { none: {} } } }); // users with no posts
await db.post.count({ where: { author: { is: { email: "alice@acme.io" } } } }); // Alice's postsCyphra has no _count or groupBy for per-row relation counts. Use db.$cypher with COUNT { }.
It converts Neo4j integers to JavaScript numbers, so the result type is honest:
const rows = await db.$cypher<{ email: string; posts: number; followers: number }>`
MATCH (u:User)
RETURN u.email AS email,
COUNT { (u)-[:AUTHORED]->(:Post) } AS posts,
COUNT { (u)<-[:FOLLOWS]-(:User) } AS followers
ORDER BY followers DESC, email
`;
// [{ email: "alice@acme.io", posts: 3, followers: 2 }, …]For small lists, loading the relation and reading .length also works, at the cost of fetching
every related node:
const users = await db.user.findMany({ include: { posts: { where: { published: true } } } });
const counts = users.map((u) => ({ email: u.email, published: u.posts.length }));With the plain neo4j-driver, count() comes back as a driver Integer object, not a number;
call .toNumber() on it. Cyphra’s db.$cypher and count() do that for you, and throw instead
of losing precision if a value is outside JavaScript’s safe integer range.
Related
- Pagination: the total page count is a
count() - Typed client: filters for
some,none,every,isandisNot - Typed Neo4j queries in TypeScript
- Glossary: graph traversal