Database Manual / Reference / Query Language / Query Predicates / Logical

$or

$or
The $or operator performs a logical OR operation on an array of one or more <expressions> and selects the documents that satisfy at least one of the <expressions>.$or运算符对一个或多个<expressions>数组执行逻辑OR运算,并选择满足至少一个<express>的文档。

Compatibility兼容性

You can use $or for deployments hosted in the following environments:您可以将$or用于在以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The $or operator 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 the quantity field value is less than 20 or the price field value equals 10.此查询将选择inventory收款中quantity字段值小于20price字段值等于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 $or expression, all the clauses in the $or expression must be supported by indexes. Otherwise, MongoDB will perform a collection scan.也就是说,MongoDB要使用索引来计算$or表达式,$or表达式中的所有子句都必须得到索引的支持。否则,MongoDB将执行集合扫描。

When using indexes with $or queries, each clause of an $or can use its own index. Consider the following query:当使用带有$或查询的索引时,$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. 如果$or包含$text查询,则$or数组中的所有子句都必须由索引支持。This is because a $text query must use an index, and $or can only use indexes if all its clauses are supported by indexes. If the $text query cannot use an index, the query will return an error.这是因为$text查询必须使用索引,而$or只能在其所有子句都受索引支持的情况下使用索引。如果$text查询不能使用索引,则查询将返回错误。

Note

$text provides text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, MongoDB Search.$text为自我管理(非Atlas)部署提供文本查询功能。对于托管在MongoDB上的数据,MongoDB还提供了改进的全文查询解决方案MongoDB搜索

$or and Geospatial Queries地理空间查询

$or supports geospatial clauses. $or支持地理空间子句However, if you use a near clause ($near or $nearSphere), $or cannot contain any other clauses. 但是,如果使用near子句($near$nearSphere),$or不能包含任何其他子句。Using $or with a single clause has the same effect as omitting the $or operator.使用$or和单个子句与省略$or运算符具有相同的效果。

For example, the following query is invalid because the $or operator combines $near with another clause:例如,以下查询无效,因为$or运算符将$near与另一个子句组合在一起:

db.places.find( {
$or: [
{ location: { $near: [ 40, 5 ] } },
{ category: "restaurant" }
]
} )

The following query is valid because $or uses a non-near geospatial clause ($geoIntersects):以下查询有效,因为$or使用了非近地理空间子句($geoIntersects):

db.places.find( {
$or: [
{
location: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [
[ [ 39, 4 ], [ 41, 4 ], [ 41, 6 ], [ 39, 4 ] ]
]
}
}
}
},
{ category: "restaurant" }
]
} )

$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. Use the partialFilterExpression of the db.collection.createIndex() method to create a partial index.您可以使用$or创建部分索引。使用db.collection.createIndex()方法的partialFilterExpression创建部分索引。

$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字段值等于2050的所有文档,请使用$in运算符:

db.inventory.find ( { quantity: { $in: [20, 50] } } )

Nested $or Clauses嵌套的$or子句

You may nest $or operations.您可以嵌套$or操作。

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 to true.在提供给$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:以下查询包含提供给$or的多个表达式,如果存在任何$x0的文档,则可能会产生错误:

db.example.find( {
$or: [
{ x: { $eq: 0 } },
{ $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } }
]
} )