Mongo 查找查询不适用于某些值。
Mongo find query is not working for certain values.
我写了一个 Mongo 搜索查询,其中包含一个 OR 运算符。它的布尔表达式如下。
A 和(B 或 C)
对于上面的表达式布局,如果 B 或 C 之一为假,它将 return 为假。因此我的查询失败了。更具体的Mongo查询如下。
{ "fieldName" : { "regex" : "AB.*" },
"$and" : [[{"fieldName2" : "$in" : ["abc", "cds"]},
"$or" : [{"fieldName3" : "$in" : ["abc", "cds"]}]]
}
在上面的查询中,如果 fieldName2
有 abc
之一 cds
但 fieldName3
没有,则查询 returns false 尽管它在 OR 子句中。
那么有没有其他方法可以实现这个搜索查询,如在提到的表达式中或者我在这里做错了什么。 ?
$and
和 $or
对其数组值的内容应用布尔运算。所以你的查询应该重写为:
{
"fieldName" : { "regex" : "AB.*" },
"$or": [{"fieldName2" : "$in" : ["abc", "cds"]},
{"fieldName3" : "$in" : ["abc", "cds"]}]
}
请注意,您甚至不需要在此处使用 $and
,因为多个查询词是隐式与运算的。
我写了一个 Mongo 搜索查询,其中包含一个 OR 运算符。它的布尔表达式如下。
A 和(B 或 C) 对于上面的表达式布局,如果 B 或 C 之一为假,它将 return 为假。因此我的查询失败了。更具体的Mongo查询如下。
{ "fieldName" : { "regex" : "AB.*" },
"$and" : [[{"fieldName2" : "$in" : ["abc", "cds"]},
"$or" : [{"fieldName3" : "$in" : ["abc", "cds"]}]]
}
在上面的查询中,如果 fieldName2
有 abc
之一 cds
但 fieldName3
没有,则查询 returns false 尽管它在 OR 子句中。
那么有没有其他方法可以实现这个搜索查询,如在提到的表达式中或者我在这里做错了什么。 ?
$and
和 $or
对其数组值的内容应用布尔运算。所以你的查询应该重写为:
{
"fieldName" : { "regex" : "AB.*" },
"$or": [{"fieldName2" : "$in" : ["abc", "cds"]},
{"fieldName3" : "$in" : ["abc", "cds"]}]
}
请注意,您甚至不需要在此处使用 $and
,因为多个查询词是隐式与运算的。