cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 1: replicate users.

Return full node after aggregation function

I need to use an aggregation function like `min()` , but instead of returning the result of the function, I need the full node as I then need to use one of the node's property in a subsequent MATCH statement. I've tried using the following query:

````

MATCH (n) WHERE n.uid IN $uids

WITH n as nodes, apoc.agg.minItems(n, n.level).items as highestNode WITH nodes, apoc.agg.first(highestNode).tree_id AS highestTreeId

MATCH (nodes)-[:TRANSLATES_TO]-(a) WHERE a.tree_id = highestTreeId return nodes, a

`

This uses the `minItems()` apoc function but the problem is it does not return a list and rather a separate map for each node processed. The result of the minItems function looks like this, for 3 nodes processed. I then need the first map in this function to be used in the next match statement:

Screenshot 2022-11-21 at 10.17.17.png

I've thought about using reduce or list comprehension but not sure how that would work. What's the common approach to this problem?

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

You got a map per node because the apoc.agg.minItem is an aggregate function and you have 'with n, apoc.agg.minItem()', so its grouping the rows with the same value of 'n' and passing them to the aggregate function. This result in one result per node 'n'.

I am not sure I understand your requirement from the code, but I took a stab at it using list comprehension instead of apoc functions. You can try this. Let me know if something is wrong and we can try to fix it.

match(n)
with collect({node:n, level:n.level}) as nodes, min(n.level) as min
with nodes, [x in nodes where x.level = min][0].node as minNode
unwind nodes as node
with node.node as n, minNode
match (n)-[:TRANSLATES_TO]-(minNode)
return n, minNode

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

You got a map per node because the apoc.agg.minItem is an aggregate function and you have 'with n, apoc.agg.minItem()', so its grouping the rows with the same value of 'n' and passing them to the aggregate function. This result in one result per node 'n'.

I am not sure I understand your requirement from the code, but I took a stab at it using list comprehension instead of apoc functions. You can try this. Let me know if something is wrong and we can try to fix it.

match(n)
with collect({node:n, level:n.level}) as nodes, min(n.level) as min
with nodes, [x in nodes where x.level = min][0].node as minNode
unwind nodes as node
with node.node as n, minNode
match (n)-[:TRANSLATES_TO]-(minNode)
return n, minNode

Thanks for the explanation. This was the query I used in the end:

`

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
`