Include Null check in MongoRepository query by example

落爺英雄遲暮 提交于 2019-12-10 16:29:42

问题


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);

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();
}

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.

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())

Many Thanks


回答1:


As per the documentation the Null-handling setting is scoped to the ExampleMatcher (not the property path). So, long story short, you can't.



来源:https://stackoverflow.com/questions/40527289/include-null-check-in-mongorepository-query-by-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!