mongorepository查询条件_通过示例在MongoRepository查询中包括Null检查

I Am trying to find all from a collection that match certain search criteria using MongoRepository using the following method:

Page findAll(Example 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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值