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'?