使用mongoTemplate进行Aggregation聚合查询

聚合框架
聚合框架是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息。

聚合管道操作主要包含下面几个部分:

命令 功能描述
$project 指定输出文档里的字段.
$match 选择要处理的文档,与fine()类似。
$limit 限制传递给下一步的文档数量。
$skip 跳过一定数量的文档。
$unwind 扩展数组,为每个数组入口生成一个输出文档。
$group 根据key来分组文档。
$sort 排序文档。
$geoNear 选择某个地理位置附近的的文档。
$out 把管道的结果写入某个集合。
$redact 控制特定数据的访问。
$lookup 多表关联(3.2版本新增)
详细请看:
链接: [link]https://www.cnblogs.com/xuliuzai/p/10055535.html)
在这里插入图片描述

1.
Criteria criteria = getCriteria(materialInfoDTO,userVo);

        Query query = new Query();
        query.addCriteria(criteria);
        long sumCount = mongoTemplate.count(query, MaterialInfo.class);

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.unwind("moldingProcessList"),
                Aggregation.match(criteria),
                Aggregation.group("moldingProcessList.code")
                        .count().as("count"));

        List<Map> result =  mongoTemplate.aggregate(aggregation,"p_company_material_info",Map.class).getMappedResults();
private Criteria getCriteria(MaterialInfoDTO materialInfoDTO, UserVo userVo) {
        Criteria criteria = new Criteria();
        if(StringUtils.isNotEmpty(materialInfoDTO.getSupplierName())){
            criteria.and("supplierName").is(materialInfoDTO.getSupplierName());
        }
        if(materialInfoDTO.getSearch() != null){
            String search = mongoUtil.escapeExprSpecialWord(materialInfoDTO.getSearch());
            Pattern pattern = Pattern.compile("^.*" + search + ".*$", Pattern.CASE_INSENSITIVE);
            criteria.orOperator(
                    Criteria.where("materialName").regex(pattern),
                    Criteria.where("materialBrand").regex(pattern),
                    Criteria.where("materialBrand").regex(pattern),
                    Criteria.where("importantIndicators.modelName").regex(pattern));
        }
        return criteria;
    }
2.
Criteria criteria = Criteria.where("userId").is(userId);
    Integer pageSize = 10;
    Integer startRows = (pageNum - 1) * pageSize;
    if(buyerNick != null && !"".equals(buyerNick)){
        criteria.and("buyerNick").is(buyerNick);
    }
    if(phones != null && phones.size() > 0){
        criteria.and("mobile").in(phoneList);
    }
    if(itemId != null && !"".equals(itemId)){
        criteria.and("orders.numIid").is(itemId);
    }
    Aggregation customerAgg = Aggregation.newAggregation(
            Aggregation.project("buyerNick","payment","num","tid","userId","address","mobile","orders"),
            Aggregation.match(criteria),
               Aggregation.unwind("orders"),
            Aggregation.group("buyerNick").first("buyerNick").as("buyerNick").first("mobile").as("mobile").
            first("address").as("address").sum("payment").as("totalPayment").sum("num").as("itemNum").count().as("orderNum"),
            Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC, "totalPayment"))),
            Aggregation.skip(startRows),
            Aggregation.limit(pageSize)
            );
    List<CustomerDetail> customerList = tradeRepository.findAggregateList(new Query(criteria), userId, customerAgg,CustomerDetail.class);
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值