springdata mongodb 使用Aggregation进行聚合查询
仅用于个人学习总结,纯属于个人理解,如有错误请谅解,顺便帮忙指出
match 匹配 直接拼好Criteria即可
MatchOperation match =null;
if (a){
match=Aggregation.match(Criteria.where("aa").in(aa).and("bb").in(bb).orOperator(new Criteria().where("cc").mod(2, 1),new Criteria().where("dd").is("dd")));
}else {
match=Aggregation.match(Criteria.where("aa").in(aa).and("bb").in(bb).and("cc").mod(2, 0));
}
groupOperation 将结果按照ee分组返回ee组最后一条数据修改返回属性名为ff 分组count 返回属性名改为tt
GroupOperation groupOperation = Aggregation.group("ee").last("ee").as("ff").count().as("tt");
创建查询并返回结果
Aggregation aggregation=Aggregation.newAggregation(match,groupOperation);
collectionName查询集合名 map.class将结果封装进Map中,也可以封装进入collectionName对应实体类
AggregationResults results = mongoTemplate.aggregate(aggregation, collectionName,Map.class);
List list = results.getMappedResults();