mongoTemplate查询

//设置分页
Integer pageSize = po.getPageSize();
Integer startRows = (po.getPage() - 1) * pageSize;

//模糊查询
Criteria criteria = new Criteria();
if (!StringUtils.isEmpty(po.getKeyword())){
    String regex = String.format("%s%s%s", "^.*", po.getKeyword().trim(), ".*$");
    Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    criteria = Criteria.where("teacherName").regex(pattern);
}

Aggregation customerAgg = Aggregation.newAggregation(
        Aggregation.project("teacherId", "messageId", "teacherName"), //至关于select
        Aggregation.match(criteria), //查询条件至关于where
        Aggregation
                .group("teacherId").first("teacherId").as("teacherId") //分组
                .first("messageId").as("id") //as起别名对应实体字段
                .first("teacherName").as("name")
                .count().as("replyNum"),//组内统计数
Aggregation.sort(Sort.by(Sort.Order.asc("messageId"))),//排序    
Aggregation.sort(Sort.by(Sort.Order.asc("messageId"))),
        Aggregation.skip(startRows),
        Aggregation.limit(pageSize)

);

AggregationResults<AnswerQueVo> aggregate = mongoTemplate.aggregate(customerAgg, TableConstant.USER_TALK_INFOR, AnswerQueVo.class);
List<AnswerQueVo> list = aggregate.getMappedResults();
相关文章
相关标签/搜索