Updated March 6, 2023
Definition of MongoDB Filter
Mongodb filter aggregation is added in version 3.2, it is used to select a subset of the array and return the result using the specified condition. The filter operator will return an array of elements that matched the specified condition, it will return the elements in the original order. We need to pass input, as and cond parameters with filter operator, input and cond are mandatory parameters while “as” is optional parameter while using filter operator in MongoDB. At the time of using the filter operator in MongoDB we need to use $ sign before the filter keyword.
Syntax:
Below is the syntax of the filter operator in MongoDB.
{$filter: {input: <array used as input string>, as: <The type of as keyword is string>, cond: <expression is used to evaluate the array elements>}}
Parameter description syntax of filter operator in MongoDB.
1) Filter – The filter operator is used to return the result using specified conditions. We have used a filter operator in MongoDB to filter the result as per the condition which was we have given in the query.
2) Input – This is an expression that was used to resolves in an array. In the input parameter, we have passed the array field to filter the documents. We need to use the $ sign before using the input field parameter in the filter operator.
3) As – It is an optional parameter used in the filter operator. This is the variable name that was used as an element in the input array. The type of this parameter in the filter operator is a string. This expression is accessed every element from an input array by using the “as” keyword.
4) Cond – This parameter is used to determine where we should include the element from the resulting array. The type of this parameter in the filter operator is expression. We have using expression to filter the data from the specified collection. We can use multiple conditional operators to filter the data from the array elements.
How filter work in MongoDB?
MongoDB filter is used to filter the data from an array using the specified condition which was we have used in our query. If the document array which was contains the empty value then the result using the filter operator will return the empty array. The below example shows that the filter operator will return an empty array set when our collection documents contain an empty array.
Code:
db.filter_test.find ()
db.filter_test.aggregate ({$project: { highmarks: { $filter: { input: "$marks", as: "marks", cond: { $gt: [ "$$marks", 10 ] } } } } } )
Figure – Filter operator will returns the empty set of documents in result.
- In the above example, we can see that second documents have an empty set of array, so after using the filter on this field it will return the empty array set.
- I suppose we have used a filter on the field which did not contain the array elements then the filter operator will return an error in the output.
- If the field of array element contains the null values instead of array elements then the result of this array filed using filter operator is null values.
- Using the filter operator we have also use the condition to filter the array elements to retrieve the data.
- We can use the below conditional operator on the array field using the filter operator in MongoDB.
1) Greater than ((>) or $gt)
2) less than ((<) or $lt)
3) Greater than equal to ((>=) or $gte)
4) Less than equal to ((<=) or $lte)
Using the above condition we can retrieve the data as per the condition which was we have used in our query.
I suppose we have applied a filter on the field which was not present in the collection then the result of the array element contains the null value.
In the below example, we can see that we have applied a filter on the marks1 field, but this field is not present in filter_test collection. So the result of this field using filter operator contains a null value.
Code:
db.filter_test.aggregate({$project: { highmarks: { $filter: { input: "$marks1", as: "marks1", cond: { $gt: [ "$$marks1", 10 ] } } } } } )
db.filter_test.find ()
Figure – Filter operator returns null values, if array field is not exist into the collection.
Examples
The below example shows the filter operator in MongoDB.
1) Filter operator to filter the array elements using greater than condition
In the below example, we have used a filter operator with greater than condition to filter the data from array elements. We have used filter operator on marks field and used greater than a condition to retrieve marks from array elements which is greater than 65.
Code:
db.filter_test.find ()
db.filter_test.aggregate({$project: { highmarks: { $filter: { input: "$marks", as: "marks", cond: { $gt: [ "$$marks", 65 ] } } } } } )
Figure – Example to filter operator to filter the array elements using greater than condition.
2) Filter operator to filter the array elements using less than condition
In the below example, we have used a filter operator with less than a condition to filter the data from array elements. We have used filter operator on marks field and used less than a condition to retrieve marks from array elements which is less than 75.
Code:
db.filter_test.find ()
db.filter_test.aggregate({$project: { highmarks: { $filter: { input: "$marks", as: "marks", cond: { $lt: [ "$$marks", 75 ] } } } } } )
Figure – Example to filter operator to filter the array elements using less than condition.
3) Filter operator to filter the array elements using greater than equal to condition
In the below example, we have used the filter operator on marks field and used greater than equal to condition to retrieve marks from array elements which is greater than equal to 65.
Code:
db.filter_test.find ()
db.filter_test.aggregate({$project: { highmarks: { $filter: { input: "$marks", as: "marks", cond: { $gte: [ "$$marks", 65 ] } } } } } )
Figure – Example to filter operator to filter the array elements using greater than equal to condition.
Conclusion
MongoDB filter operator is used to filter the elements from the array field. We can use multiple condition like $gt, $lt, $gte and $lte to retrieve data from array elements. I suppose we have used filter operator on empty array field it will return the empty result in output.
Recommended Articles
This is a guide to MongoDB Filter. Here we discuss the definition, syntax, How filters work in MongoDB? Examples, and code implementation. You may also have a look at the following articles to learn more –