Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-24-2019 10:04 AM
I'm trying to write a query that iterates over a collection of nodes by their index (so that I can iterate two lists in tandem). I'm not getting expected results.
For example, if I have 10 nodes with label "Node" in my graph and I query MATCH (n:Node) WITH n, collect(n) AS myNodes RETURN size(myNodes)
I expect to get one result
size(myNodes)
10
Instead, I get 10 results
size(myNodes)
1
1
1
...
I expected from the answer to this SO question that I should be able to iterate over the results of a collect()
function by the index.
Solved! Go to Solution.
07-24-2019 10:44 AM
Change:
MATCH (n:Node) WITH n, collect(n) AS myNodes RETURN size(myNodes)
To:
MATCH (n:Node) WITH collect(n) AS myNodes RETURN size(myNodes)
Elaine
07-24-2019 10:44 AM
Change:
MATCH (n:Node) WITH n, collect(n) AS myNodes RETURN size(myNodes)
To:
MATCH (n:Node) WITH collect(n) AS myNodes RETURN size(myNodes)
Elaine
07-24-2019 10:56 AM
Thank you, Elaine.
Just so that I understand this better... querying WITH n, collect(n)
is creating a list of the collection (with just one element), where WITH collect(n)
creates the list of the results. Is that what's going on?
07-24-2019 11:23 AM
Aggregation in Cypher is with respect to the set of non-aggregation variables when you perform the aggregation, which forms the grouping key.
In your query, WITH n, collect(n)
, this means collecting n
with respect to each n
. n
is the grouping key. You'll get an n
per row, and the collection of just itself in the list.
When you do WITH collect(n)
, there are no other variables besides the aggregation, so it collects over everything, and you get a single row with the entire collected set of nodes.
All the sessions of the conference are now available online