通过示例

时间:2016-11-10 12:09:56

标签: java spring mongodb spring-data spring-data-mongodb

我试图使用以下方法使用MongoRepository从符合特定搜索条件的集合中查找所有内容:

<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);

为此,我正在为搜索中包含的所有字段构建ExampleMatcher。像这样:

 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的最后一个检查。我似乎无法在文档中找到有关如何解决此问题的任何信息。

我已尝试在提供的示例模型中添加以下nullField为null,但这似乎被忽略了。

.withMatcher("nullField", matcher -> matcher.exact())

非常感谢

1 个答案:

答案 0 :(得分:0)

根据documentation,Null处理设置的作用域为ExampleMatcher(而不是属性路径)。所以,长话短说,你不能。

相关问题