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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With