Search code examples
neo4jcypherneo4j-apoc

Cypher APOC - Aggregations should not be used like this


I have the following query:

MATCH (n) WHERE n.uid IN $uids
WITH n as nodes, apoc.agg.minItems(n, n.level).items as highestNode
MATCH (nodes)-[:TRANSLATES_TO]-(a) where a.tree_id = apoc.agg.first(highestNode).tree_id
return nodes, a

I'm getting the error Aggregations should not be used like this. This is happening when introducing the first function on the third line of the query. minItems returns a map with the same key for each node so I need to get the first element of the map somehow. What minItems returns:

enter image description here


Solution

  • I used the min() function and then used the value of that in a list comprehension like this:

    match (n) where n.uid in $uids
    with collect(n) as nodes, min(n.level) as min
    with nodes, [x in nodes where x.level = min][0] as minNode
    unwind nodes as node
    match (node)-[:TRANSLATES_TO]-(a) where a.tree_id = minNode.tree_id
    return node, a