Drop the nodes we collect

We find that writing and maintaining Cypher queries is a little painful when it comes to collecting items. We often want to collect something and discard the original node. The following is an example:

MATCH (p)-[]-(c) WITH p, collect(c) as c RETURN p, c

The above does not look too bad. The problem is the explicit name of p , the field we want to keep. As we add more MATCH and OPTIONAL MATCH with aggregation, this becomes a maintainability nightmare. We cannot reorder MATCH / WITH pairs without changing also all the fields that we reference. When we do collect , we always want to discard the original node.

WITH has * which can be used, but this will include the field we are collecting, and we cannot replace the value.

MATCH (p)-[]-(c) WITH *, collect(c) as c RETURN p, c

Is there a way to exclude something in a WITH statement without explicitly naming everything that should be included? Something like the following?

MATCH (p)-[]-(c) WITH *, without(c), collect(c) as cs RETURN p, cs

+5
source share
1 answer

I don’t think there is a way to do this, and given how clusters work in Cypher, I think it will cause a lot more headaches than it will cure.

Remember that aggregation only makes sense for non-aggregate columns that form a grouping key. I believe that explicitly including variables in WITH clauses is invaluable, as it gives you an idea of ​​which fields are in scope, which gives you context for the aggregation columns.

Without this, you risk erroneous behavior, because in the area you forgot to exclude, there may be variables that will change the value and calculate your clusters.

0
source

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


All Articles