Spring Data MongoDB 多个可选参数的 AND/OR 查询

7
我正在尝试执行一个有超过2个可选参数的查询,但是我没有得到任何结果。对于这2个参数,我遵循了这个问题的答案:spring-data-mongo - optional query parameters?
因此,例如,对于以下查询,一切都正常:
@Query("{$and: [{$or : [ { $where: '?0 == null' } , { a : ?0 } ]}, {$or : [ { $where: '?1 == null' } , { b : ?1 }]}]}")

但如果我添加一个参数,它就停止工作了:

@Query("{ $and : [{$and: [{$or : [ { $where: '?0 == null' } , { a : ?0 } ]}, {$or : [ { $where: '?1 == null' } , { b : ?1 }]}]},{$or : [ { $where: '?2 == null' } , { c : ?2 }]}]}")

我三次检查了语法,看起来没问题,但是结果为空(即使我确定应该至少获得一些文档)。
有什么想法吗?
1个回答

8
如果您仔细手动格式化查询语句使其更易读,您会注意到在括号的闭合方面可能犯了一些错误。
请尝试使用以下查询语句代替:
{ $and : 
    [{
       $and: 
        [
         {$or : [ { $where: '?0 == null' } , { a : ?0 }]}, 
         {$or : [ { $where: '?1 == null' } , { b : ?1 }]},
         {$or : [ { $where: '?2 == null' } , { c : ?2 }]}
        ]
    }]
}

侧面说明:我认为一个$and就足够了,也就是移除顶层的$and操作符。

谢谢!我不知道$and运算符可以与超过2个元素一起使用。它可以工作,而且更易读 :-) - Alexio Cassani
这里有没有办法使用 $regex?因为我无法让它工作。 - Sercan Ozdemir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接