Skip to content

Reduce aggregation operation doesn't allow using Variable.VALUE and Variable.THIS with strict mapping #4070

@Cerber-Ursi

Description

@Cerber-Ursi

I've attempted to execute the following aggregation operation:

{
  "names": {
    "$reduce": {
      "input": "$nestedNames",
      "initialValue": "$names",
      "in": {
        "$setUnion": [
          "$$value",
          "$$this"
        ]
      }
    }
  }
}

where the original structure is like this:

public class NamesLists {
  public List<String> names;
  public List<List<String>> nestedNames;
}

This, if I'm not mistaken, should correspond to the following Spring code:

AggregationOperation project = Aggregation.project()
    .and(
        ArrayOperators.Reduce.arrayOf("nestedNames")
            .withInitialValue(Fields.field("names"))
            .reduce(SetUnion.arrayAsSet(Variable.VALUE.getTarget()).union(Variable.THIS.getTarget()))
    )
    .as("names");

This, however, doesn't work with AggregationOptions.strictMapping(), since Variable.VALUE and Variable.THIS are converted into $$value and $$this correspondingly, and document obviously doesn't have these fields.

This aggregation works without strict mapping, since in this case the variable names are passed into the document as-is. However, this is merely masking the problem.


It's possible that I was just incorrectly using the variables. In this case, it's probably better to have the documentation changed, since currently it seems that the only references to them are in documentation for ReduceBuilder.Reduce.

Activity

Cerber-Ursi

Cerber-Ursi commented on Jun 3, 2022

@Cerber-Ursi
Author

Got similar problem also with $map operation. The following code works (with the same base model) only without strict mapping:

AggregationOperation project = Aggregation.project()
    .and(
        VariableOperators.mapItemsOf("names")
            .as("name")
            .andApply(StringOperators.valueOf("$$name").lengthCP())
    )
    .as("ownersLength");

Otherwise, it fails with "No property '$$name'".

changed the title [-]Reduce aggregation operation doesn't allow using Variable.VALUE and Variable.THIS with strict mapping[/-] [+]Reduce aggregation operation doesn't allow using `Variable.VALUE` and `Variable.THIS` with strict mapping[/+] on Jan 12, 2023
added 3 commits that reference this issue on Aug 17, 2023
added this to the 4.1.3 (2023.0.3) milestone on Aug 17, 2023
added a commit that references this issue on May 20, 2024
f1cff3c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @mp911de@christophstrobl@Cerber-Ursi@spring-projects-issues

    Issue actions

      Reduce aggregation operation doesn't allow using `Variable.VALUE` and `Variable.THIS` with strict mapping · Issue #4070 · spring-projects/spring-data-mongodb