How the score (nodes (p)) works in Cypher, Neo4j

I am looking for an explanation of how this works, and why it does not return the number of nodes in the path. Suppose I map path p. Now:

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

returns 1.

When it is clear, how to count path nodes correctly?

+5
source share
1 answer

count() is an aggregate function . When using any aggregate function, the resulting rows will be grouped by all RETURN and non- aggregated functions included in the sentence. In this case, the result rows will be grouped by p , and the return value will be counted (nodes (p)).

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

To return the number of nodes in a path, you should use size(nodes(p)) .

If you are just interested in the length of the path and not especially in the nodes that are included in it, I would recommend that you use length(p) . This will return the length in rels for the given path without the need for manipulation / access to nodes.

+6
source

Source: https://habr.com/ru/post/1215662/


All Articles