Skip to main content

$filter

The $filter operator filters elements from an array based on a provided condition.

Input

  • An array containing an array to filter and a condition to evaluate for each element

Output

  • A new array containing only the elements that satisfy the condition

Examples

Filter Even Numbers

{
  "$filter": [
    [1, 2, 3, 4, 5, 6],
    {
      "$eq": [
        {
          "$mod": [
            {
              "$var": ["current"]
            },
            2
          ]
        },
        0
      ]
    }
  ]
}
Result: [2, 4, 6]

Filter From Field

{
  "$filter": [
    {
      "$input": "products"
    },
    {
      "$gt": [
        {
          "$var": ["current"]
        },
        100
      ]
    }
  ]
}
Result: An array of products with values greater than 100