我使用 Spring Data (1.3.0.RC1) 来访问我们的 MongoDB,对于一些新的查询,我想使用聚合框架。
在 mongo shell 中,命令是:
db.spotreports.aggregate(
{ "$unwind" : "$pd"} , { "$group" : { "_id" : "$pd.PDch", "base" : {$sum : "$pd.aBL"}, "uplift" : {$sum : "$pd.up"}}})
{
"result" : [
{
"_id" : 5,
"base" : 529133,
"uplift" : 21516
},
...
我使用的 Spring 代码是:
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);
我收到以下错误:
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)
我想知道为什么spring要引用'PDch'而不是'pd.PDch'?