代码之家  ›  专栏  ›  技术社区  ›  YasBES

如何自定义聚合。匹配(…)MongoDB For Java

  •  1
  • YasBES  · 技术社区  · 7 年前

    我需要在此示例中自定义聚合匹配条件(注意: filters 是我需要在中实现的所有条件的列表 $match ):

     public static void filterQueryForSuppWarning(QueryParamDto queryParamDto, List<Bson> filters) {
            if (queryParamDto != null) {
                if (!CollectionUtils.isEmpty(queryParamDto.getUets())) {
                    filters.add(eq(ID_UET.getLabel(),queryParamDto.getUets()));
                }
                if (!CollectionUtils.isEmpty(queryParamDto.getMgtgroups())) {
                    filters.add(eq(PN_MANAGEMENT_GROUP.getLabel(), queryParamDto.getMgtgroups()));
                }
                if (!CollectionUtils.isEmpty(queryParamDto.getSuppliers())) {
                    filters.add(eq(SupplierFieldEnum.SUP_NAME.getLabel(), queryParamDto.getSuppliers()));
                }
                if (!StringUtils.isEmpty(queryParamDto.getReference())) {
                    filters.add(eq(ID_PN_PARTNUMBER.getLabel(), queryParamDto.getReference()));
                }
                if (!CollectionUtils.isEmpty(queryParamDto.getSupplierAddressCodes())) {
                    filters.add(eq(SupplierFieldEnum.CODE_SUPPLIER_DLAP_SITE.getLabel(), queryParamDto.getSupplierAddressCodes()));
                }
                if (!CollectionUtils.isEmpty(queryParamDto.getSupplierAccounts())) {
                    filters.add(eq(SupplierFieldEnum.ID_SUP_NUMBER.getLabel(), queryParamDto.getSupplierAccounts()));
                }
                if (!StringUtils.isEmpty(queryParamDto.getAppReference())) {
                    filters.add(eq(APP_REFERENCE.getLabel(), queryParamDto.getAppReference()));
                }
    
                if (!CollectionUtils.isEmpty(queryParamDto.getWarnings())) {
                    for (String code : queryParamDto.getWarnings()) {
                        filters.add(eq(code.toLowerCase(),1));
                    }
                }
            }
        }
    

    的问题 aggregate.match 不支持 List<Bson> .

    我如何在我的查询中实现许多类似于此示例的条件:

    AggregateIterable<Document> result = collection.aggregate(Arrays.asList(
        Aggregates.match(and(eq("dt_extract","2017-07-03"), eq("id_pn_partnumber",queryParamDto.getReference()))),
        Aggregates.project(include("id_sup_number","sup_name",                                       "group_name","code_supplier_dlpa_site","idPnPartNumber"))
    ));
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Community CDub    4 年前

    Filters.and() 接受iterable(至少从v3.2开始)。从 Javadoc :

    public static Bson and(Iterable<Bson> filters)

    创建一个过滤器,该过滤器对提供的过滤器列表执行逻辑“与”。请注意,这将仅在绝对必要时生成“$和”运算符,因为查询语言隐式将所有键组合在一起。

    因此,以下代码将起作用:

    List<Bson> filters = new ArrayList<>();
    
    // populate filters, as per your filterQueryForSuppWarning() 
    // ...
    
    AggregateIterable<Document> result = collection.aggregate(Arrays.asList(
            // wrap your list of filters in an 'and' and assign that directly to the $match stage
            Aggregates.match(Filters.and(filters)),
            Aggregates.project(Projections.include("id_sup_number", "sup_name",
                    "group_name", "code_supplier_dlpa_site", "idPnPartNumber"))
    ));