非常使用的mongodb的聚合函数(使用SpringDataMongoDb)

来源:互联网 发布:长沙源码信息有限公司 编辑:程序博客网 时间:2024/05/17 01:00

下面这一段就是用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 (...)


原创粉丝点击