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();
相關文章
相關標籤/搜索