$or
On this page本页内容
$or
-
The$or
operator performs a logicalOR
operation on an array of one or more<expressions>
and selects the documents that satisfy at least one of the<expressions>
.$or
运算符对一个或多个<expressions>
的数组执行逻辑OR
运算,并选择至少满足其中一个<expritions>
的文档。The$or
has the following syntax:$or
具有以下语法:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
Consider the following example:考虑以下示例:db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
This query will select all documents in the此查询将选择inventory
collection where either thequantity
field value is less than20
or theprice
field value equals10
.inventory
集合中quantity
字段值小于20
或price
字段值等于10
的所有单据。
Behaviors行为
$or
Clauses and Indexes子句和索引
When evaluating the clauses in the 在评估$or
expression, MongoDB either performs a collection scan or, if all the clauses are supported by indexes, MongoDB performs index scans. $or
表达式中的子句时,MongoDB会执行集合扫描,或者,如果所有子句都有索引支持,MongoDB则会执行索引扫描。That is, for MongoDB to use indexes to evaluate an 也就是说,为了让MongoDB使用索引来计算$or
expression, all the clauses in the $or
expression must be supported by indexes. $or
表达式,$or
表达式中的所有子句都必须由索引支持。Otherwise, MongoDB will perform a collection scan.否则,MongoDB将执行集合扫描。
When using indexes with 在对$or
queries, each clause of an $or
can use its own index. Consider the following query:$or
查询使用索引时,$or
的每个子句都可以使用自己的索引。请考虑以下查询:
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
To support this query, rather than a compound index, you would create one index on 要支持此查询,而不是复合索引,您需要创建一个quantity
and another index on price
:quantity
索引和另一个price
索引:
db.inventory.createIndex( { quantity: 1 } )
db.inventory.createIndex( { price: 1 } )
$or
and 和text
Queries查询
If 如果$or
includes a $text
query, all clauses in the $or
array must be supported by an index. This is because a $text
query must use an index, and $or
can only use indexes if all its clauses are supported by indexes. $or
包含$text
查询,则索引必须支持$or
数组中的所有子句。这是因为$text
查询必须使用索引,而$or
只能在索引支持其所有子句的情况下使用索引。If the 如果$text
query cannot use an index, the query will return an error.$text
查询无法使用索引,则该查询将返回错误。
$or
and GeoSpatial Queries和GeoSpatial查询
$or
supports geospatial clauses with the following exception for the near clause (near clause includes $nearSphere
and $near
). $or
cannot contain a near clause with any other clause.$or
支持地理空间子句,但near子句除外(near子句包括$nearSphere
和$near
)。$or
不能与任何其他子句一起包含near子句。
$or
and Sort Operations和排序操作
When executing 当使用$or
queries with a sort()
, MongoDB can use indexes that support the $or
clauses.sort()
执行$or
查询时,MongoDB可以使用支持$or
子句的索引。
$or
and Partial Indexes和部分索引
You can create partial indexes with 您可以使用$or
. $or
创建部分索引。Use the 使用partialFilterExpression
of the db.collection.createIndex() method to create a partial index.db.collection.createIndex()
方法的partialFilterExpression
创建分部索引。
$or
versus $in
$or
与$in
$or
versus $in
When using 当将$or
with <expressions>
that are equality checks for the value of the same field, use the $in
operator instead of the $or
operator.$or
与对同一字段的值进行相等检查的<expressions>
一起使用时,请使用$in
运算符而不是$or
运算符。
For example, to select all documents in the 例如,要选择inventory
collection where the quantity
field value equals either 20
or 50
, use the $in
operator:inventory
集合中quantity
字段值等于20
或50
的所有文档,请使用$in
运算符:
db.inventory.find ( { quantity: { $in: [20, 50] } } )
Nested $or
Clauses嵌套$or
子句
$or
ClausesYou may nest 您可以嵌套$或操作。$or
operations.
Error Handling错误处理
To allow the query engine to optimize queries, 为了允许查询引擎优化查询,$or
handles errors as follows:$or
按如下方式处理错误:
If any expression supplied to如果提供给$or
would cause an error when evaluated alone, the$or
containing the expression may cause an error but an error is not guaranteed.$or
的任何表达式在单独计算时会导致错误,则包含该表达式的$or
可能会导致错误但不能保证会出错。An expression supplied after the first expression supplied to在提供给$or
may cause an error even if the first expression evaluates totrue
.$or
的第一个表达式之后提供的表达式可能会导致错误,即使第一个表达式的计算结果为true
。
For example, the following query always produces an error if 例如,如果$x
is 0
:$x
为0
,则以下查询总是会产生错误:
db.example.find( {
$expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] }
} )
The following query, which contains multiple expressions supplied to 如果存在$or
, may produce an error if there is any document where $x
is 0
:$x
为0的任何文档,则包含提供给$or
的多个表达式的以下查询可能会产生错误:
db.example.find( {
$or: [
{ x: { $eq: 0 } },
{ $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } }
]
} )