3

I use Spring Data (1.3.0.RC1) to access our MongoDB and for some new queries I want to use the aggregation framework.

In the mongo shell the command would be:

db.spotreports.aggregate(
 { "$unwind" : "$pd"} , { "$group" : { "_id" : "$pd.PDch", "base" : {$sum : "$pd.aBL"}, "uplift" : {$sum : "$pd.up"}}})
{
"result" : [
{
"_id" : 5,
"base" : 529133,
"uplift" : 21516
},
...

The Spring Code I use is:

Aggregation agg = Aggregation.newAggregation(
Aggregation.unwind("pd"),
Aggregation.group("pd.PDch").sum("pd.aBL").as("base").sum("pd.up").as("uplift")
);

AggregationResults<SpotReportResult> result = mongoTemplate.aggregate(agg, resolveCollection(), SpotReportResult.class);

I get the following error:

java.lang.IllegalArgumentException: Invalid reference 'PDch'!
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:63)
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:47)
at org.springframework.data.mongodb.core.aggregation.GroupOperation.toDBObject(GroupOperation.java:284)
at org.springframework.data.mongodb.core.aggregation.Aggregation.toDbObject(Aggregation.java:228)

I wonder why spring want to reference 'PDch' instead of 'pd.PDch'?

sme
  • 33
  • 1
  • 4
  • This looks like a bug. Any chance you can file an issue and provide a minimal test case (i.e. domain class and aggregation definition)? – Oliver Drotbohm Sep 11 '13 at 03:49

2 Answers2

1

The problem is not PDch or pd.PDch. For some reason the code is not using the Aggregation.DEFAULT_CONTEXT to get the field reference. This is happens when ever there is more than one field. Code works correctly if you only have one group field.

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
1

According to DATAMONGO-753 This should be fixed in the latest snapshots and released with the upcoming 1.3.2 bugfix release and the upcoming 1.4 M1 of Spring Data MongoDB.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211