$in
The $in
operator selects the documents where the value of a field equals any value in the specified array. $in
运算符选择字段值等于指定数组中任何值的文档。To specify an 要指定$in
expression, use the following prototype:$in
表达式,请使用以下原型:
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
For comparison of different BSON type values, see the specified BSON comparison order.有关不同BSON类型值的比较,请参阅指定的BSON比较顺序。
If the 如果field
holds an array, then the $in
operator selects the documents whose field
holds an array that contains at least one element that matches a value in the specified array (for example, <value1>
, <value2>
, and so on).field
包含数组,则$in
运算符选择其field
包含数组的文档,该数组至少包含一个与指定数组中的值匹配的元素(例如<value1>
、<value2>
等)。
The $in
operator compares each parameter to each document in the collection, which can lead to performance issues. $in
运算符将每个参数与集合中的每个文档进行比较,这可能会导致性能问题。To improve performance:要提高性能:
$in
operator to tens of values. $in
运算符的参数数量限制为几十个值。field
you want to query.field
上创建索引。This document describes the 本文档介绍了$in
query operator. $in
查询运算符。For the 有关$in
aggregation operator, see $in (aggregation).$in
聚合运算符,请参阅$in
(聚合)。
Create the 创建inventory
collection:inventory
集合:
db.inventory.insertMany( [ { "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] }, { "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] }, { "item": "Maps", "tags": [ "office", "storage" ] }, { "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] } ] )
$in
Operator to Match Values$in
运算符匹配值Consider the following example:考虑以下示例:
db.inventory.find( { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } )
This query selects all documents in the 此查询选inventory
collection where the value of the quantity
field is either 5 or 15.inventory
集合中quantity
字段值为5或15的所有文档。
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, { item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
Although you can write this query using the 尽管可以使用$or
operator, use the $in
operator rather than the $or
operator when performing equality checks on the same field.$or
运算符编写此查询,但在对同一字段执行相等性检查时,请使用$in
运算符而不是$or
运算符。
$in
Operator to Match Values in an Array$in
运算符匹配数组中的值The following 当updateMany()
operation sets the exclude
field to false
when the tags
array has at least one element that matches either "home"
or "school"
.tags
数组至少有一个元素与“home”或“school”匹配时,以下updateMany()
操作将exclude
字段设置为false
。
db.inventory.updateMany( { tags: { $in: [ "home", "school" ] } }, { $set: { exclude: false } } )
Example output:示例输出:
{ item: 'Pens', quantity: 350, tags: [ 'school', 'office' ], exclude: false }, { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ], exclude: false }, { item: 'Maps', tags: [ 'office', 'storage' ] }, { item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ], exclude: false }
For additional examples in querying arrays, see:有关查询数组的其他示例,请参阅:
For additional examples in querying, see:有关查询的其他示例,请参阅:
$in
Operator with a Regular Expression$in
运算符与正则表达式一起使用The $in
operator can specify matching values using regular expressions of the form /pattern/
. $in
运算符可以使用/pattern/
形式的正则表达式指定匹配值。You cannot use 不能在$regex
operator expressions inside an $in
.$in
中使用$regex
运算符表达式。
Consider the following example:考虑以下示例:
db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )
This query selects all documents in the 此查询选择inventory
collection where the tags
field holds either a string that starts with be
or st
or an array with at least one element that starts with be
or st
.inventory
集合中的所有文档,其中tags
字段包含以be
或st
开头的字符串,或者包含至少一个以be
或st
开头的元素的数组。