Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does count(nodes(p)) work in Cypher, Neo4j

Tags:

neo4j

cypher

I'm looking for an explanation of how this works and why doesn't return the number of nodes in a path. Suppose I matched a path p. Now:

WITH p, count(nodes(p)) AS L1 RETURN L1

returns 1.

When this is clear, how do I count paths nodes properly?

like image 782
wiktus239 Avatar asked Mar 19 '15 09:03

wiktus239


1 Answers

count() is an aggregate function. When using any aggregate function, result rows will be grouped by whatever is included in the RETURN clause and not an aggregate function. In this case, result rows will be grouped by p and the return value will be count(nodes(p)).

nodes(p) returns an array of nodes, so count(nodes(p)) will return the count of arrays and will always equal 1.

In order return the amount of nodes in the path you should use size(nodes(p)).

If you're just interested in the length of a path and not particularly in the nodes that are included in it, I would encourage you to use length(p). This will return the length in rels for a given path, without having to manipulate/access the nodes.

like image 114
albertoperdomo Avatar answered Sep 22 '22 00:09

albertoperdomo