zoukankan      html  css  js  c++  java
  • mongodb mongotemplate聚合

    1.group by并且计算总数

      @Test
        public void insertTest() {
            //测试数据
            //insertTestData();
            Aggregation agg = Aggregation.newAggregation(
                    //Aggregation.match(Criteria.where("groupId").is(5)),
                    Aggregation.group("groupId").count().as("total"), 
                    Aggregation.project("total").and("groupId").previousOperation(),            
                    Aggregation.sort(Sort.DEFAULT_DIRECTION, "total"));
            List<StaCount> list = mongoTemplate.aggregate(agg, "currentUser", StaCount.class).getMappedResults();
            for(StaCount count : list) {
                System.out.println(count.getGroupId() + ":" + count.getTotal());
            }
      }
    如果要带其他字段,将红字变为
    Aggregation.group("groupId","groupName"...),并将project的and部分去掉即可;
    
    

     2.Crieria的使用,注意andOperator和orOperator的用法

    Aggregation agg = Aggregation.newAggregation(

        Aggregation.match(new Criteria()

                .andOperator(Criteria.where("onlineTime").gt(new Date()))

                .orOperator( Criteria.where("offlineTime").gt(new Date())

        ,Criteria.where("offlineTime").exists(false) ))

     3.Query的排序和分页

    Query query = new Query(Criteria.where(ReportField.GROUP_ID).in(groupIdList));
            query.with(new Sort(Direction.DESC, ReportField.ID));
            query.skip(lastId).limit(limit);
  • 相关阅读:
    CentOS7 网络管理相关命令
    CentOS7配置双网卡绑定
    ajax请求
    django向数据库添加数据
    django基于正则的url匹配
    django前端到后端一次简单完整的请求实例
    django数据库动态添加列
    django1.9 创建项目和app并初始化项目
    django1.9 创建数据表
    数据结构和算法(5)-树
  • 原文地址:https://www.cnblogs.com/guochunyi/p/5315943.html
Copyright © 2011-2022 走看看