Introduction to MongoDB $filter Operator

The $filter operator is an array operator in MongoDB. It filters elements from an array based on a specified condition and returns a new array containing all elements that meet the condition.

Syntax

The syntax of the $filter operator is as follows:

{ $filter: { input: <array>, as: <string>, cond: <expression> } }

Where:

  • input: The input array.
  • as: The name of the variable assigned to each element.
  • cond: The filter condition, which is an expression that evaluates to true or false.

Use Cases

The $filter operator is commonly used in the following scenarios:

  • To return only the array elements that meet specific criteria in a query result.
  • To filter the elements of an array during aggregation operations.

Examples

Example 1

The following aggregation pipeline uses the $filter operator to filter elements in the scores array that are greater than or equal to 90:

db.students.aggregate([
  {
    $project: {
      _id: 0,
      name: 1,
      passedScores: {
        $filter: {
          input: "$scores",
          as: "score",
          cond: { $gte: ["$$score", 90] }
        }
      }
    }
  }
])

Suppose the students collection has the following documents:

{
  _id: 1,
  name: "Alice",
  scores: [90, 80, 95]
},
{
  _id: 2,
  name: "Bob",
  scores: [75, 60, 85]
}

The result of running the above aggregation pipeline is:

{
  "name": "Alice",
  "passedScores": [ 90, 95 ]
},
{
  "name": "Bob",
  "passedScores": [ 85 ]
}

Example 2: Filtering Array Elements

The following aggregation pipeline uses the $filter operator to filter elements in the orderItems array that have a product name of “A”:

db.orders.aggregate([
  {
    $project: {
      _id: 0,
      orderNumber: 1,
      productAItems: {
        $filter: {
          input: "$orderItems",
          as: "item",
          cond: { $eq: ["$$item.product", "A"] }
        }
      }
    }
  }
])

Suppose the orders collection has the following documents:

{
  orderNumber: "2022030101",
  orderItems: [
    { product: "A", quantity: 2 },
    { product: "B", quantity: 1 }
  ]
}
{
  orderNumber: "2022030102",
  orderItems: [
    { product: "C", quantity: 3 },
    { product: "A", quantity: 1 },
    { product: "D", quantity: 2 }
  ]
}

The result of running the above aggregation pipeline is:

{
  "orderNumber": "2022030101",
  "productAItems": [
    { "product": "A", "quantity": 2 }
  ]
}
{
  "orderNumber": "2022030102",
  "productAItems": [
    { "product": "A", "quantity": 1 }
  ]
}

In this example, the input parameter of the $filter operator specifies the array to be filtered, the as parameter specifies an alias for each array element during the filtering process, and the cond parameter specifies the filtering condition. Through this aggregation pipeline, we obtain a new document where the productAItems array contains only elements with the product name “A”.

Conclusion

By using the $filter operator, we can filter elements in an array within an aggregation pipeline that meet specific conditions. This operator is a convenient way to filter and process documents that contain arrays without writing complex logic. Using the $filter operator requires specifying an input array, a placeholder for the current element, and a condition expression. The expression must return a Boolean value; elements that evaluate to true are retained, while those that evaluate to false are filtered out.

In practice, the $filter operator is often used in conjunction with other aggregation pipeline operators, such as $project, $group, and so on. During data processing, these operators can be combined to create complex aggregation pipelines to achieve more flexible and efficient data analysis and processing.