MongoDB Filter Query

MongoDB filter aggregation was introduced in version 3.2. It is used to choose a subset of an array and provide the result based on criteria. The filter operator returns an array of entries that fit the provided criterion in the order they were found. When using the filter operator in MongoDB, we must provide the input, as and cond as arguments. Input and cond are required parameters, however “as” is optional. We must use the $ sign before the filter keyword when we use the filter operator in MongoDB.

Syntax of the Filter Query MongoDB in Ubuntu 20.04?

The $filter operator provides an array with just those elements in the original order that match the condition. Mentioned below is the syntax of $filter:

{ $filter: { input: <array_field>, as: <string>, cond: <expression> } }
  • filter: The $filter operator is used to provide a result that meets certain criteria. In MongoDB, we used the filter operator to filter the results based on the criterion we specified in the query.
  • input: This is an array resolution expression. We managed to pass the array field as an input parameter to filter the records. Before we use the input field parameter in the filter operator, we must use the $ sign.
  • as: It’s a filter operator argument that’s optional. This is the field’s name which was utilized as an element in the input array. This parameter is of the string type in the filter operator. Using the “as” keyword, this expression accesses each element in an input array.
  • cond: This option determines where the value from the generated array should be included. This parameter is of the expression type in the filter operator. The values from the given collection were filtered using expression. We can use numerous conditional operators to filter data from array elements.

How the Filter Query works in MongoDB in Ubuntu 20.04:

A MongoDB $filter operator is used to filter documents from an array based on a condition that we defined in our query. Based on the given condition, returns a subset of an array. Only those array entries that satisfy the requirements are returned. The elements are reassembled in the same arrangement as before.

Let’s look at some instances to help you understand what we are discussing. As we proceed through the article, we would like you to try the examples in the mongo shell. We start by creating a database collection that utilizes throughout the article.

Here, we have defined a collection of the database as “candidates” by using the MongoDB shell. For this collection, we have the id, name, and marks field defined. We have inserted seven documents at the same time by using the insertMany query.

The collection “candidates” is invoking the find function as in the above query. Then, the recorded document displays the field and its corresponding values. Note that in the field “marks”, the array is defined with some random values.

Example 1:Using the $filter operator in MongoDB in Ubuntu 20.04:

The $filter operator is used to filter the array members in the field “marks” from the given collection in the following example:

We have utilized the aggregate query on the collection of “candidates”. Inside this query, we have the $match operator which is used to filter the document by the id. We have assigned the id field a $in operator. The $in operator finds documents where the value of id equals any value in the provided array. Then, the $filter operator is passed the input argument and sets its value “$marks”. The “as” parameter is also used which returns the field name “marks”. Then, we have specified the cond parameter that uses the greater than operator on the field “$$marks”.

By following the above query, the result we got is shown in the figure with the new field name highmarks.

Example 2:Using the $filter operator for an empty array in MongoDB in Ubuntu 20.04:

When one of our collection documents has an empty array, the filter operator returns an empty array set, as can be seen here:

Within the $match we have specified the id that has the $in operator which returns id “7” from the collection as we have given “7” inside an array. Then, the $filter query has provided the collection field “marks” which returns an empty array because no value is stored inside the “marks” field of id “7”.

The result of the filter operator has an empty set of the document here.

Example 3: Using the $filter operator for the wrong type in MongoDB in Ubuntu 20.04:

If we try to use the $filter on a field that doesn’t have an array, we will get an error. The following is the demonstration of this statement.

We have given a field “name” from the document whose id is “5” inside the $filter operator. In the parameter cond of $filter, we have the $eq operator which takes the field “$name” and matches the string value defined to it.

When we run the above query, the error message shows that the type must be an array in the $filter operator as seen in the figure.

Example 4: Using the $filter operator with the $this variable in MongoDB in Ubuntu 20.04:

We used the “as” parameter to give the variable a name in the earlier cases. The as parameter is optional. If we set this field blank, the variable name will be “this” by default.

Here, we have specified the array in $in operator which selects the id fields from the documents of the given collection within the $match. Then, the $filter has the two-parameter, input and cond. The cond parameter has the greater than operator which retrieves the marks greater than 65. We haven’t used them as a parameter, instead we utilized the $$this field name.

Hence by using $$this to reference the field, we got the same results as from the “as”. The output is satisfied with the $match condition and the $filter condition seen in the picture.


This is a MongoDB Filter guide. In this article, we have gone through the definition, syntax, and how filters function in MongoDB. Examples and code implementation are also provided. The array field’s elements are filtered using the MongoDB filter operator. To retrieve data from array elements, we have utilized several conditions such as $gt, $lt, $gte, and $lte.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.