我正在尝试执行具有两个以上可选参数的查询,但没有得到任何结果。对于2个参数我遵循了这个问题的答案spring-data-mongo - 可选查询参数? https://stackoverflow.com/questions/11613464/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 }]}]}")
我三次检查了语法,看起来没问题,但我得到了空结果(即使我确信我应该至少获得一些文档)。
任何想法?
如果您尝试仔细地手动设置查询格式以使其更具可读性,您会发现您在右括号方面犯了一些错误。
请尝试以下查询:
{ $and :
[{
$and:
[
{$or : [ { $where: '?0 == null' } , { a : ?0 }]},
{$or : [ { $where: '?1 == null' } , { b : ?1 }]},
{$or : [ { $where: '?2 == null' } , { c : ?2 }]}
]
}]
}
旁注:我认为一个$and
就足够了,即删除顶层$and
操作员。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)