Mongodb Mongo find查询对某些值无效。
我编写了一个Mongo搜索查询,其中包含OR运算符。其布尔表达式如下所示 A和(B或C) 对于表达式的上述布局,如果B或C中的一个设置为false,它将返回false。因此,我的查询失败了。更具体的Mongo查询如下所示Mongodb Mongo find查询对某些值无效。,mongodb,boolean-expression,Mongodb,Boolean Expression,我编写了一个Mongo搜索查询,其中包含OR运算符。其布尔表达式如下所示 A和(B或C) 对于表达式的上述布局,如果B或C中的一个设置为false,它将返回false。因此,我的查询失败了。更具体的Mongo查询如下所示 { "fieldName" : { "regex" : "AB.*" }, "$and" : [[{"fieldName2" : "$in" : ["abc", "cds"]}, "$or" : [{"fieldName3" : "$in" : ["abc", "c
{ "fieldName" : { "regex" : "AB.*" },
"$and" : [[{"fieldName2" : "$in" : ["abc", "cds"]},
"$or" : [{"fieldName3" : "$in" : ["abc", "cds"]}]]
}
在上面的查询中,如果fieldName2
有一个abc
或者cds
,但是fieldName3
没有,那么查询返回false,尽管它位于或子句中
那么,有没有其他方法可以实现上述表达式中的搜索查询,或者我在这里做错了什么
$和
和$或
对其数组值的内容应用布尔运算。因此,您的查询应重写为:
{
"fieldName" : { "regex" : "AB.*" },
"$or": [{"fieldName2" : "$in" : ["abc", "cds"]},
{"fieldName3" : "$in" : ["abc", "cds"]}]
}
请注意,您甚至不需要在此处使用
$和,因为多个查询词都是隐式和-ed的。我们是否可以使用springdata mongo条件纠正此问题?@JawadhSalihRifath我不使用spring,因此如果需要,最好将其作为一个新问题发布。