如何自定义 Aggregate.match(...) MongoDB For Java
How to customize Aggregate.match(...) MongoDB For Java
我需要在这里自定义聚合匹配条件的例子(注意: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"))
));
Filters.and()
接受一个可迭代对象(至少从 v3.2 开始)。来自 Javadoc:
public static Bson and(Iterable<Bson> filters)
Creates a filter that performs a logical AND of the provided list of filters. Note that this will only generate a "$and" operator if absolutely necessary, as the query language implicity ands together all the keys.
因此,以下代码将起作用:
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"))
));
我需要在这里自定义聚合匹配条件的例子(注意: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"))
));
Filters.and()
接受一个可迭代对象(至少从 v3.2 开始)。来自 Javadoc:
public static Bson and(Iterable<Bson> filters)
Creates a filter that performs a logical AND of the provided list of filters. Note that this will only generate a "$and" operator if absolutely necessary, as the query language implicity ands together all the keys.
因此,以下代码将起作用:
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"))
));