为了以布尔逻辑实现此操作,我执行以下操作并将其转换为编程语言中可用的操作
:query != null -> field == :query!(:query != null) || (field == :query)(:query == null) || (field == :query)
| |
在普通的SQL中,这是通过
where (null = :query) or (field = :query)
在MongoDB中,这是通过$ where完成的
{ $where: '?0 == null || this.field == ?0' } 通过使用Mongo Operations,我们可以稍微加快速度,而不是以牺牲一些可读性为代价来构建该函数的所有内容。不幸的是无法正常工作。
{ $or : [ { $where: '?0 == null' } , { field : ?0 } ] } 所以你拥有的是
@Query("{ $or : [ { $where: '?0 == null' } , { field : ?0 } ] }")List<Something> findAll(String query, Pageable pageable);可以进一步扩展以处理in / all子句的数组
@Query("{ $or : [ { $where: '?0.length == 0' } , { field : { $in : ?0 } } ] }")List<Something> findAll(String query, Pageable pageable);


