- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Array Aggregation Operators >
- $filter (aggregation)
$filter (aggregation)¶
On this page
Definition¶
-
$filter¶ New in version 3.2.
Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.
$filterhas the following syntax:Field Specification inputAn expression that resolves to an array. asOptional. A name for the variable that represents each individual element of the inputarray. If no name is specified, the variable name defaults tothis.condAn expression that resolves to a boolean value used to determine if an element should be included in the output array. The expression references each element of the inputarray individually with the variable name specified inas.For more information on expressions, see Expressions.
Behavior¶
| Example | Results |
|---|---|
[ 1, 2, 3.1, NumberLong(4) ] |
Example¶
A collection sales has the following documents:
The following example filters the items array to only include
documents that have a price greater than or equal to 100:
The operation produces the following results: