[数据库]MongoTemplate之分组分页复合条件查询

mongodbsql 文档地址: https://docs.mongodb.com/manual/reference/operator/update/

废话不多说,直接上代码


情景一:分组查询

 public Page<CourseDetail> listCourseDetails(QueryCourseDetailModel queryModel) {
​
        //条件一 where gradeId in (集合) and mark=true
        Criteria criteria = Criteria.where("gradeId").in(gradeIds).and("mark").is(true);
if (null != status && status.size() > 0) {
            Criteria criteriaChild = new Criteria();
            //条件二  criteriaChild 条件查询 ( teacherCommentStatus = XX or parentsReplyTeacherStatus =XX  )
            criteriaChild.orOperator(Criteria.where("teacherCommentStatus").in(status), Criteria.where("parentsReplyTeacherStatus").is(replayStatus));
        }
​
        //拼接最后语句 where gradeId in (集合) and mark=true  and ( teacherCommentStatus = XX or parentsReplyTeacherStatus =XX  )
        criteria.andOperator(criteriaChild);

        //分组条件
        GroupBy groupBy = new GroupBy("gradeId")
                .initialDocument("{ count: 0 }")
                .reduceFunction("function (doc,pre){pre.count +=1 ;}");

        //使用 mongoTemplate.group 分组查询
        GroupByResults groupByResults = mongoTemplate.
                group(criteria, "homework", groupBy, Homework.class);

        //获取结果
        BasicDBList list = (BasicDBList) groupByResults.getRawResults().get("retval");
        list.stream().map(map -> {
            BasicDBObject obj = (BasicDBObject) map;
            ......
            return obj;
        }).collect(Collectors.toList());
return list;

        ​}

情景二: 分页查询

public Page<CourseDetail> listCourseDetails(QueryCourseDetailModel queryModel) {
​
        //创建排序模板Sort
        Sort sort = new Sort(Sort.Direction.DESC, "creationDate");//创建分页模板Pageable 
        Pageable pageable = new PageRequest(queryModel.getPageStart(), queryModel.getPageSize(), sort);

        //创建查询条件对象 
        Query query = new Query();//条件gradeId =XX
        Criteria criteria = Criteria.where("gradeId").is(queryModel.getGradeId());

        //条件 and time > XX 
        if (!StringUtil.isEmpty(queryModel.getStartCourseTime())) {
            criteria.and("time").gt(queryModel.getStartCourseTime());
        }​
        //条件 and time < XX if(! StringUtil.isEmpty(queryModel.getEndCourseTime())){
        criteria.and("time").lt(queryModel.getEndCourseTime());

        query.addCriteria(criteria);//mongoTemplate.count计算总数 
        long total = mongoTemplate.count(query, CourseDetail.class);

        // mongoTemplate.find 查询结果集
        List<CourseDetail> items = mongoTemplate.find(query.with(pageable), CourseDetail.class);items.forEach(courseDetail -> {
            courseDetail.setTeacherUnReplaySize(homeworkRepository.countByGradeIdAndParentsReplyTeacherStatusAndCourseStartTimeAndMarkTrue(courseDetail.getGradeId(), PARENTS_REPLY_TEACHER_STATUS.UNANSWERED, courseDetail.getCourseStartTime().getTime()));
        });
        
        return new PageImpl(items, pageable, total);
        
        ​}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源14

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值