Spring Data MongoDB和/或带有多个可选参数的查询 [英] Spring Data MongoDB AND/OR query with multiple optional parameters

查看:826
本文介绍了Spring Data MongoDB和/或带有多个可选参数的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用2个以上的可选参数执行查询,但我没有得到任何结果。对于这两个参数,我按照这个问题的答案 spring-data-mongo - 可选的查询参数?

I'm trying to execute a query with more than 2 optional parameters but I don't get any result. For the 2 parameters I followed the answer of this question spring-data-mongo - optional query parameters?.

例如,通过以下查询,一切正常:

So for example, with the following query everything is ok:

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

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

But if I add one more parameter it stops to work:

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

我检查了三次语法并且看起来没问题,但我得到空结果(即使我确定我应该至少获得少量文档)。

I triple checked the syntax and seems ok, but I get empty results (even if I'm sure I should obtain at least few documents).

任何想法?

推荐答案

如果你试试c你可以手动格式化查询以便更具可读性,你会注意到你用右括号犯了一些错误。

If you try carefully hand-format you query to be more readable, you will note that you have made a few mistakes with the closing brackets.

请尝试此查询:

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






附注:我认为一个 $和就足够了,即删除顶级 $和运算符。


Side note: I think one $and will be enough, i.e. remove the top-level $and operator.

这篇关于Spring Data MongoDB和/或带有多个可选参数的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆