Database Manual / Reference / Query Language / Query Predicates / Comparison

$in

$in
The $in operator selects the documents where the value of a field equals any value in the specified array.$in运算符选择字段值等于指定数组中任何值的文档。

Compatibility兼容性

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

  • 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语法

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. To improve performance:$in运算符将每个参数与集合中的每个文档进行比较,这可能会导致性能问题。为了提高性能:

  • It is recommended that you limit the number of parameters passed to the $in operator to tens of values. Using hundreds of parameters or more can negatively impact query performance.建议将传递给$in运算符的参数数量限制为数十个值。使用数百个或更多参数会对查询性能产生负面影响。
  • Create an index on the field you want to query.在要查询的field上创建索引。

Note

This document describes the $in query operator. For the $in aggregation operator, see $in (expression operator).本文档介绍$in查询运算符。关于$in聚合运算符,请参阅$in(表达式运算符)

Query Data on Atlas by Using MongoDB Search使用MongoDB搜索在Atlas上查询数据

For data stored in MongoDB Atlas, you can use the MongoDB Search in Operator operator when running $search queries. Running $in after $search is less performant than running $search with the in Operator operator.对于存储在MongoDB Atlas中的数据,您可以在运行$Search查询时使用MongoDB搜索in Operator运算符。在$search之后运行$in的性能不如使用in Operator运算符运行$search

To learn more about the MongoDB Search version of this operator, see the in Operator operator in the Atlas documentation.要了解有关此运算符的MongoDB搜索版本的更多信息,请参阅Atlas文档中的in运算符

Examples示例

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" ] }
] )

Use the $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集合中数量字段值为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运算符。

Use the $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".当标签数组中至少有一个元素与"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: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}

For additional examples on querying arrays, see:有关查询数组的其他示例,请参阅:

For additional examples on querying, see Query Documents.有关查询的其他示例,请参阅查询文档

Use the $in Operator with a Regular Expression在正则表达式中使用$in运算符

The $in operator can specify matching values using regular expressions of the form /pattern/. You cannot use $regex operator expressions inside an $in.$in运算符可以使用/pattern/形式的正则表达式指定匹配值。不能在$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字段包含以best开头的字符串或至少有一个以best开头的元素的数组。