Java Spring MongoDB自定义筛选器忽略空参数

Java Spring MongoDB自定义筛选器忽略空参数,spring,mongodb,null,Spring,Mongodb,Null,我正在尝试使用自定义表达式和多个字段进行筛选。 对于名称字段,我使用正则表达式。 我检查所有其他字段是否包含在参数列表中 @Query(value = "{$and:[" + "{'name': {$regex: ?0, $options: 'i'}}," + "{'types': {$in: ?1}}," + "{'customer': {$in: ?2}}" + "]}") Optional<List<Object

我正在尝试使用自定义表达式和多个字段进行筛选。 对于名称字段,我使用正则表达式。 我检查所有其他字段是否包含在参数列表中

@Query(value = 
    "{$and:["
        + "{'name': {$regex: ?0, $options: 'i'}},"
        + "{'types': {$in: ?1}},"
        + "{'customer': {$in: ?2}}"
    + "]}")
Optional<List<Object>> findByFilter(
            @Param("name") String name,
            @Param("types") Collection<String> types,
            @Param("customers") Collection<String> customers,
            Pageable pageable);
@查询(值=
“{$and:[”
+“{'name':{$regex:?0,$options:'i'},”
+{'types':{$in:?1}}
+“{'customer':{$in:?2}}”
+ "]}")
可选findByFilter(
@参数(“名称”)字符串名称,
@参数(“类型”)集合类型,
@Param(“客户”)收款客户,
可寻呼(可寻呼);
如果参数为null,如何忽略? 我尝试了spring表达式语言,但这不起作用

@Query(value = 
    "{$and:["
        + "{'name': {$regex: ?0, $options: 'i'}},"
        + "{'types': {$in: ?1}},"
        + "?#{ [2] != null ? {'customer': {$in: [2]}} }"
    + "]}")
Optional<List<Object>> findByFilter(
            @Param("name") String name,
            @Param("types") Collection<String> types,
            @Param("customers") Collection<String> customers,
            Pageable pageable);
@查询(值=
“{$and:[”
+“{'name':{$regex:?0,$options:'i'},”
+{'types':{$in:?1}}
+“?#{[2]!=null?{'customer':{$in:[2]}”
+ "]}")
可选findByFilter(
@参数(“名称”)字符串名称,
@参数(“类型”)集合类型,
@Param(“客户”)收款客户,
可寻呼(可寻呼);

我不确定这是否可行,但建议您使用比
MongoRepository
语法更灵活的语法

@Autowired
private MongoTemplate mongotemplate;

Criteria criteria = Criteria.where("name").regex(name).and("types").in(types);
if (customers != null || !customers.isEmpty()) {
    criteria.and("customers").in(customers);
}

List<YourClass> result = mongotemplate.find(Query.query(criteria).with(pageable), YourClass.class);
@Autowired
私有MongoTemplate MongoTemplate;
标准=标准。其中(“名称”).regex(名称)和(“类型”).in(类型);
if(customers!=null | |!customers.isEmpty()){
(客户)中的标准和(“客户”);
}
List result=mongotemplate.find(Query.Query(条件).with(pageable),YourClass.class);