There, the output is obtained with an array of documents, each containing a details field that includes the first element from the original details array where the marks are greater than or equal to
80
.
3. Using the $filter operator with the $$this operator
When using the
$filter
, we can access the value of the current element being processed using the special variable $$this. This allows us to apply conditions and comparisons to the current element.
# Usage of $filter operator with the $$this operator
db.student.aggregate([
$match: { _id: { $in: [ 1, 2, 4 ] } }
$project: {
AssignmentPoints: {
$filter: {
input: "$assignmentMarks",
cond: { $lt: [ "$$this", 8 ] }
Here, the aggregation pipeline is deployed in two stages. The first stage is
$match
, which filters the documents based on a condition. It matches documents where the
_id
field is either 1, 2, or 4. Then, the second stage is the
$project
, where the
$filter
operator is called to filter the elements of the
assignmentMarks
array. Further, it takes a cond parameter that specifies the condition that each element in the
assignmentMarks
array must satisfy to be included in the filtered array. The conditions are based on the value of the current element being processed by referencing it with the $this variable.
Hence, the output yielded as expected from the above filter query.
4. Using the $filter operator for an empty array
In addition to the aforementioned illustrations, when using the
$filter
operator in MongoDB, if the input array is empty, the
$filter
operator will return an empty array as a result. There, we perform the
$filter
operator on the empty array.
# Usage of $filter operator for an empty array
db.student.aggregate([
$match: { _id: { $in: [ 3 ] } }
$project: {
highPoints: {
$filter: {
input: "$assignmentMarks",
as: "points",
cond: { $gt: [ "$$points", 9 ] }
The
$filter
operator is employed here to filter the elements of the
assignmentMarks
array. It takes the required parameters to query the filter document. Note that the condition is
{ $gt: [ "$$points", 9 ] }
, but since there are no elements in the input array, the condition is never evaluated.
As a result, the output displayed an empty array of a document.
5. Conclusion
In conclusion, the
$filter
query operator in MongoDB provides a powerful tool for working with array elements. It allows you to perform conditional filtering on arrays within documents during the aggregation pipeline. Here, we demonstrate the structure and the usage of the $filter operator query with examples.
More details about this topic can be found
here
.
You may also like reading:
-
MongoDB SQL Join with Examples
-
Secure MongoDB with Username and Password
-
E11000 duplicate key error index in MongoDB
-
MongoDB Add a New Field to All Documents
-
MongoDB Search in Array of Objects
-
Run MongoDB as a Window Service
-
Import CSV file using mongoimport in MongoDB
-
MongoDB find() with all ($all Operator)
-
Node.js Using Express.js with MongoDB
-
Most Useful Basic MongoDB Commands