【发布时间】:2016-05-23 14:54:01
【问题描述】:
我有以下数据:
{groupId: 1, name: Jon},
{groupId: 1, name: Dan},
{groupId: 2, name: Jon},
{groupId: 2, name: Ris},
{groupId: 3, name: David}
我收到一个 groupID 数组作为输入,我想计算这些组的 DISTICT 名称总数,我将聚合代码定义如下:
groupIds [] = {1,2}
Aggregation agg = newAggregation(
match(Criteria.where("groupId").in((groupIds)),
group("name").count().as("total")
);
但我得到一个 groupResult 包含每个名称的计数,即:
{name : Jon, total : 2}
{name : Dan, total : 1}
{name: Ris, total : 1}
虽然我实际上想要得到的总数是 = 3(实际上是上面 groupResult 的大小)
我需要如何调整我的聚合来实现这一点?
谢谢!
附言计数中忽略了大卫 - 正如预期的那样
【问题讨论】:
标签: java mongodb aggregation-framework mongotemplate