cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

What is the best approach to use collect inside a custom object

So I need to get all :REL relationships of a specific node d:Doc but I want to return the result in a very specific way:

  • The id of the d node
  • A collection/array of nodes a with specific properties, but also I need to collect the ids into a property grouping by the other 2 properties
  • A collection/array of nodes b with specific properties

So far this approach works but there is a warning of  this being deprecated:

 

MATCH(d:Doc) 
WHERE d.id = "xxxxx"
OPTIONAL MATCH (n)-[:REL]->(d)
WITH d.id as id,
CASE WHEN n:A THEN 
{label:"A", ids: collect(n.id), type: n.type, code: n.code} END AS a,
CASE WHEN n:B THEN 
{label:"B", id: n.id, prop: n.prop } END AS b

return id, collect(a) as as, collect(b) as bs

 

warning:

 

This feature is deprecated and will be removed in future versions.

Aggregation column contains implicit grouping expressions. Aggregation expressions with implicit grouping keys are deprecated and will be removed in a future version. For example, in 'RETURN n.a, n.a + n.b + count(*)' the aggregation expression 'n.a + n.b + count(*)' includes the implicit grouping key 'n.b', and this expression is now deprecated. It may be possible to rewrite the query by extracting these grouping/aggregation expressions into a preceding WITH clause.

 

I read this post: Getting a warning when using collect 

And I came up with this, but the database will have millions of nodes, I'm not sure if this is the best approach.

 

MATCH (d:Doc)
WHERE d.id = "xxxxx"

MATCH (a:A)-[:REL]->(d)
WITH d, a.code as code, a.type as type, collect(a.id) AS ids
WITH d, {ids:ids, code:code, type: type} AS a

MATCH (b:B)-[:REL]->(d)
WITH d, e, collect({id: b.document, prop: b.prop}) AS b

RETURN d.id, collect(a), b

 

Is it possible to do it with one MATCH and using CASE like in my first query? (can't use apoc)

 

1 REPLY 1

Is b the same as a ? in the matches? are you double matching?

but yes, in general it should work like this. move implicit inner aggregations to a previous WITH.

Your last collect should be 

WITH d, collect(a) AS collectedAs, b
RETURN d.id, collectedAs, b