对于春季数据 mongo 2.1.4
如果您假设您的返回班是这样的:
class Output{
Object x;
Object y;
/**getter and setter**/
}
您可以使用以下代码:
1.Hold分组数据:
AggregationResults<Output> result =
mongoTemplate.aggregate(
agg ,
"YOUR_DOCUMENT",
Output.class
);
int count = result.getMappedResults().size();
2.仅获取计数:(分组在您使用first(...)
或last(...)
之后使用之前不会生效)
Aggregation agg = Aggregation.newAggregation(
match(criteria),
count().as("x")/*using x only for mapping count*/
);
AggregationResults<Output> result =
mongoTemplate.aggregate(
agg ,
"YOUR_DOCUMENT",
Output.class
);
int count = result.getMappedResults().get(0).getX().get(0);/**o_O**/