Java – include null check in mongorepository query by example
•
Java
I tried to use mongorepository to find everything from a collection that meets specific search criteria using the following method:
<S extends T> Page<S> findAll(Example<S> example,Pageable pageable);
To do this, I'm building an examplematcher for all the fields included in the search like this:
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(); }
But I need to add a last check that the field (such as nullfield) is empty I can't seem to find any information in the documentation about how to solve this problem
I tried to add the following nullfield to the sample model provided, but this seems to be ignored
.withMatcher("nullField",matcher -> matcher.exact())
Thank you.
Solution
According to the documentation, the scope of null processing setting is examplematcher (not property path) So, to make a long story short, you can't
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码