Skip to content

MongoTemplate.aggregateStream does not support local variables #4132

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

Closed
Saljack opened this issue Aug 2, 2022 · 1 comment
Closed

MongoTemplate.aggregateStream does not support local variables #4132

Saljack opened this issue Aug 2, 2022 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@Saljack
Copy link

Saljack commented Aug 2, 2022

Hello
I found an issue in aggregateStream method on MongoTemplate. If you use a local variable ($$variable_name) for example in filter operator then it is not able to resolve this local variable name and throws:

org.springframework.data.mapping.context.InvalidPersistentPropertyPath: No property '$$localvar' found on class ***.TraceEntity! Did you mean: ?
	at org.springframework.data.mapping.context.PersistentPropertyPathFactory.createPersistentPropertyPath(PersistentPropertyPathFactory.java:205)
	at org.springframework.data.mapping.context.PersistentPropertyPathFactory.lambda$getPersistentPropertyPath$1(PersistentPropertyPathFactory.java:172)
...

Everything works fine with the aggregate method. I think the issue is in the aggregation context because aggregateStream uses TypeBasedAggregationOperationContext and aggregate uses RelaxedTypeBasedAggregationOperationContext. Which is weird I would excepted the same aggregation context type.

Here is an example:

MyEntity:

{
  "activitaTrace" : [
     {"type": "USAGE_CLOUD"},
     {"type": "OTHER_USAGE"}
  ]
}

Aggregation

ProjectionOperation projection = project()
    .and(
        filter("activityTrace")
            .as("localvar")
            .by(
                Eq.valueOf("$$localvar.type")
                    .equalToValue("USAGE_CLOUD")))
    .as("filtered");

TypedAggregation<UsageTracking> aggregation = newAggregation(UsageTracking.class, List.of(projection));
List<Document> aggregate = mongoTemplate.aggregate(aggregation, Document.class).getMappedResults(); // It works

List<Document> aggregateStream = mongoTemplate.aggregateStream(aggregation, Document.class).stream().toList(); // throws InvalidPersistentPropertyPath

I use spring-data-mongodb 3.3.4 but I also checked the latest version (4.0.0-SNAPSHOT) and there is also the same aggregation context in the aggregateStream. So I assume the latest version is also affected.

@christophstrobl
Copy link
Member

@Saljack thanks for reporting!

mp911de pushed a commit that referenced this issue Aug 25, 2022
Align aggregation context usage of aggregate and aggregate stream methods.

Closes #4132
Original pull request: #4147.
mp911de added a commit that referenced this issue Aug 25, 2022
Refine assertions.

See #4132
Original pull request: #4147.
mp911de added a commit that referenced this issue Aug 25, 2022
Refine assertions.

See #4132
Original pull request: #4147.
@mp911de mp911de added this to the 3.4.3 (2021.2.3) milestone Aug 25, 2022
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
Development

Successfully merging a pull request may close this issue.

4 participants