Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Cerber-Ursi opened this issue May 31, 2022 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@Cerber-Ursi
Copy link

Cerber-Ursi commented May 31, 2022

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 31, 2022
@Cerber-Ursi
Copy link
Author

Cerber-Ursi commented Jun 3, 2022

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'".

@christophstrobl christophstrobl self-assigned this Aug 1, 2022
@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 1, 2022
@mp911de mp911de 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 Jan 12, 2023
mp911de added a commit that referenced this issue Aug 17, 2023
Update since tags. Add missing Override annotation.

See #4070
Original pull request: #4242
mp911de pushed a commit that referenced this issue Aug 17, 2023
This commit introduces a new AggregationVariable type that is intended to better identify variables within a pipeline to avoid mapping failures caused by invalid field names.

Closes #4070
Original pull request: #4242
mp911de added a commit that referenced this issue Aug 17, 2023
Update since tags. Add missing Override annotation.

See #4070
Original pull request: #4242
@mp911de mp911de added this to the 4.1.3 (2023.0.3) milestone Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
4 participants