通过示例在MongoRepository查询中包括Null检查 [英] Include Null check in MongoRepository query by example
问题描述
我正尝试使用以下方法使用MongoRepository从符合某些搜索条件的集合中查找所有内容:
I Am trying to find all from a collection that match certain search criteria using MongoRepository using the following method:
<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);
为此,我将为搜索中包括的所有字段构建一个ExampleMatcher.像这样:
To do this I am building an ExampleMatcher for all fields that are included in the search. like so:
private ExampleMatcher buildMatcher(FormSearchParameters formSearchParameters) {
ExampleMatcher exampleMatcher = ExampleMatcher.matching()
.withIgnorePaths("f1", "f2", "f3", "f4" , "f5");
if (!StringUtils.isEmpty(formSearchParameters.getName())) {
exampleMatcher = exampleMatcher.withMatcher("name", match -> match.regex());
}
if (!StringUtils.isEmpty(formSearchParameters.getFoo())) {
exampleMatcher = exampleMatcher.withMatcher("foo", matcher -> matcher.exact().ignoreCase());
}
if (null != formSearchParameters.getFilter()) {
exampleMatcher = exampleMatcher.withMatcher("filter", matcher -> matcher.exact());
}
else {
exampleMatcher = exampleMatcher.withIgnorePaths("filter");
}
return exampleMatcher.withIncludeNullValues();
}
但是,我需要添加最后一个检查,其中字段(例如nullField)为null.我似乎无法在文档中找到有关如何进行此操作的任何信息.
However I need to add a last check where a field (say nullField) is null. I cant seem to find any information on how to go about this in the documentation.
我尝试在提供的示例模型中添加以下内容,其中nullField为null,但这似乎被忽略了.
I have tried adding the following where nullField is null in the example model provided but this seems to be ignored.
.withMatcher("nullField", matcher -> matcher.exact())
非常感谢
推荐答案
As per the documentation the Null-handling setting is scoped to the ExampleMatcher (not the property path). So, long story short, you can't.
这篇关于通过示例在MongoRepository查询中包括Null检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!