聚合框架
聚合框架是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);
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐