spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

MongoTemplate.aggregateStream does not support local variables

Open Saljack opened this issue 2 years ago • 0 comments

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.

Saljack avatar Aug 02 '22 12:08 Saljack