$or$or对由一个或多个表达式组成的数组执行逻辑OR操作,并选择满足至少一个表达式的文档。
兼容性
可以使用 $or 查找托管在以下环境中的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
$or 操作符的语法如下:
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
本页上的示例使用sample_mflix示例数据集中的数据。有关如何将此数据集加载到自管理MongoDB 部署中的详细信息,请参阅加载示例数据集。如果对示例数据库进行了任何修改,则可能需要删除并重新创建数据库才能运行本页上的示例。
考虑以下示例:
db.movies.find( { $or: [ { runtime: { $gt: 1000 } }, { year: { $lt: 1910 } } ] }, { _id: 0, title: 1, year: 1, runtime: 1 } )
[ { runtime: 11, title: 'The Great Train Robbery', year: 1903 }, { runtime: 14, title: 'A Corner in Wheat', year: 1909 }, { runtime: 1256, title: 'Centennial', year: 1978 }, { runtime: 1140, title: 'Baseball', year: 1994 }, { runtime: 1, title: 'The Kiss', year: 1896 }, { runtime: 1, title: 'The Kiss', year: 1896 } ]
此查询选择 movies集合中满足以下任一条件的所有文档:
runtime字段值大于1000。year字段值早于1910。
行为
$or 子句和索引
对 $or 表达式中的子句求值时,MongoDB 会执行集合扫描或索引扫描。如果索引支持所有子句,则 MongoDB 会执行索引扫描。要使用索引对 $or 表达式求值,索引必须支持 $or 表达式中的所有子句。否则,MongoDB 会执行集合扫描。
将索引与 $or 查询一起使用时,$or 的每个子句都可使用自己的索引。请考虑以下查询:
db.movies.find( { $or: [ { runtime: { $gt: 1000 } }, { year: { $lt: 1910 } } ] } )
要支持此查询,请在 runtime 上创建一个索引,并在 year 上创建另一索引,而不是复合索引:
db.movies.createIndex( { runtime: 1 } ), db.movies.createIndex( { year: 1 } ),
$or 和 text 查询
如果 $or 包含 $text 查询,则 $or 数组中的所有子句都必须由索引支持。这是因为 $text 查询必须使用索引,而仅当索引支持 $or 的所有子句时,后者才能使用索引。如果 $text 查询无法使用索引,则该查询会返回错误。
注意
$text 提供自管理(非 Atlas)部署的文本查询功能。对于托管在 MongoDB Atlas 上的数据,MongoDB 提供了一种改进的全文查询解决方案 Atlas Search。
$or 和地理空间查询
$or 支持地理空间子句。但是,如果您使用 near 子句($near 或 $nearSphere),则 $or 不能包含任何其他子句。使用仅包含一个子句的 $or 与省略 $or 操作符的效果相同。
以下查询是有效的,因为 $or 使用了非邻近地理空间子句 ($geoIntersects):
db.theaters.find( { $or: [ { "location.geo": { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [ [ [ -74.5, 40.5 ], [ -73.5, 40.5 ], [ -73.5, 41.0 ], [ -74.5, 40.5 ] ] ] } } } }, { "location.address.state": "NY" } ] } )
[ { _id: ObjectId('59a47287cfa9a3a73e51e92f'), theaterId: 200, location: { address: { street1: '3124 Jericho Tpke', city: 'East Northport', state: 'NY', zipcode: '11731' }, geo: { type: 'Point', coordinates: [ -73.319092, 40.838463 ] } } }, { _id: ObjectId('59a47287cfa9a3a73e51ead6'), theaterId: 345, location: { address: { street1: '148 Walt Whitman Rd', city: 'Huntington Station', state: 'NY', zipcode: '11746' }, geo: { type: 'Point', coordinates: [ -73.410637, 40.825775 ] } } }, { _id: ObjectId('59a47287cfa9a3a73e51eae8'), theaterId: 374, location: { address: { street1: '2478 Central Park Ave', city: 'Yonkers', state: 'NY', zipcode: '10710' }, geo: { type: 'Point', coordinates: [ -73.826805, 40.983246 ] } } }, { _id: ObjectId('59a47287cfa9a3a73e51eafd'), theaterId: 384, location: { address: { street1: '40 Catherwood Road', city: 'Ithaca', state: 'NY', zipcode: '14850' }, geo: { type: 'Point', coordinates: [ -76.492142, 42.481991 ] } } }, { _id: ObjectId('59a47287cfa9a3a73e51eb2c'), theaterId: 428, location: { address: { street1: '1 Crossgates Mall Rd', city: 'Albany', state: 'NY', zipcode: '12203' }, geo: { type: 'Point', coordinates: [ -73.848686, 42.690285 ] } } } ]
$or 和排序操作
$or 和部分索引
您可以使用 $or 来创建部分索引。使用 db.collection.createIndex() 方法的 partialFilterExpression 来创建部分索引。
$or 相较于 $in
如果同时使用 $or 和针对同一字段值进行等值检查的 <expressions>,建议使用 $in 而非 $or。
此查询选择 movies 集合中的文档,其中 year 是 1903 或 1909:
db.movies.find( { year: { $in: [1903, 1909] } }, { _id: 0, title: 1, year: 1 } )
[ { title: 'The Great Train Robbery', year: 1903 }, { title: 'A Corner in Wheat', year: 1909 } ]
嵌套 $or 子句
您可以嵌套 $or 操作。
Error Handling
要允许查询引擎优化查询,$or 会按如下方式处理错误:
如果提供给
$or的任何表达式在单独求值时会导致错误,则包含该表达式的$or可能但不一定会导致错误。在提供给
$or的第一个表达式之后提供的表达式可能会导致错误,即使第一个表达式的计算结果为true。