On this page本页内容
$in
¶New in version 3.4.版本3.4中的新功能。
Returns a boolean indicating whether a specified value is in an array.返回一个布尔值,指示指定的值是否在数组中。
Note
This document describes the 本文档介绍了$in
aggregation operator. $in
聚合运算符。For the 有关$in
query operator, see $in.$in
查询运算符,请参阅$in
。
$in
has the following operator expression syntax:$in
具有以下运算符表达式语法:
<expression> |
|
<array expression> |
Unlike the 与$in
query operator, the aggregation $in
operator does not support matching by regular expressions.$in
查询运算符不同,聚合$in
运算符不支持通过正则表达式进行匹配。
{ $in: [ 2, [ 1, 2, 3 ] ] } |
true |
{ $in: [ "abc", [ "xyz", "abc" ] ] } |
true |
{ $in: [ "xy", [ "xyz", "abc" ] ] } |
false |
{ $in: [ [ "a" ], [ "a" ] ] } |
false |
{ $in: [ [ "a" ], [ [ "a" ] ] ] } |
true |
{ $in: [ /^a/, [ "a" ] ] } |
false |
{ $in: [ /^a/, [ /^a/ ] ] } |
true |
$in
fails with an error in either of the following cases: if the $in expression is not given exactly two arguments, or if the second argument does not resolve to an array.$in
在以下任何一种情况下都会失败,并出现错误:如果$in表达式没有恰好给出两个参数,或者如果第二个参数没有解析为数组。
A collection named 名为fruit
has the following documents:fruit
的集合包含以下文档:
The following aggregation operation looks at the 下面的聚合操作将查看每个文档中的in_stock
array in each document and determines whether the string bananas
is present.in_stock
数组,并确定字符串bananas
是否存在。
The operation returns the following results:操作返回以下结果: