【发布时间】:2015-04-28 09:50:04
【问题描述】:
我有以下代码 sn-p 应该使用 spring-data-mongodb 检索帐户的总数
TypedAggregation<Account> agg = Aggregation.newAggregation(Account.class,
group("user.id"),
group().count().as("total"));
AggregationResults<AccountTotal> result = mongos.aggregate(agg, AccountTotal.class);
AccountTotal account = result.getMappedResults().get(0);
account.getTotal(); // should print 90 but prints 1
这是从我在 mongo shell 打印 90 中使用的 agg 字段返回的等效 mongo 脚本
{ "$group" : { "_id" : "$user.id"}} ,
{ "$group" : { "_id" : null , "total" : { "$sum" : 1}}}
> db.accounts.aggregate(
[
{ "$group" : { "_id" : "$user.id"}} ,
{ "$group" : { "_id" : null , "total" : { "$sum" : 1}}}
])
我在 Java 平台中获得 1 时实际上缺少什么。
编辑: 将前一个更改为以下一个后,我得到了预期的计数:
Aggregation agg = Aggregation.newAggregation(
group("user.id"),
group().count().as("total"));
AggregationResults<AccountTotal> result =
mongos.aggregate(agg, this.getCollectionName(), AccountTotal.class);
顺便说一句,谢谢@chridam。
【问题讨论】:
标签: java spring mongodb aggregation-framework spring-data-mongodb