- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $in (aggregation)
$in (aggregation)¶
On this page
Definition¶
-
$in
¶ New in version 3.4.
Returns a boolean indicating whether a specified value is in an array.
$in
has the following operator expression syntax:Operand Description <expression>
Any valid expression expression. <array expression>
Any valid expression that resolves to an array. Unlike the
$in
query operator, the aggregation$in
operator does not support matching by regular expressions.Example Results { $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
Behavior¶
$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.
Example¶
A collection named fruit
has the following documents:
The following aggregation operation looks at the in_stock
array in
each document and determines whether the string bananas
is present.
The operation returns the following results: