下面这一段就是用java代码来实现mongodb的聚合函数aggrega.
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(criteria),//条件
Aggregation.group("a","b","c","d","e").count().as("f"),//分组字段
Aggregation.sort(sort),//排序
Aggregation.skip(page.getFirstResult()),//过滤
Aggregation.limit(pageSize)//页数
);
AggregationResults<Test> outputType=mongoTemplate.aggregate(agg,"test",Test.class);
List<Test> list=outputType.getMappedResults();
mongodb原语句:
db.getCollection('test').aggregate( [
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $group: { _id: null, count: { $sum: 1 } } },
{ $sort:a},
{ $skip:10},
{ $limit:10}
] );
类似于sql
select a,b,c,count(1) from test where ... group by (...)