Question of $sum in $match stage

In the manual (https://docs.mongodb.com/manual/reference/operator/aggregation/sum/), it says $sum can used in $match stage that includes an $expr expression.
However, I can’t make it work and the error message is “unknown operator: $sum”.
Can you provide a concrete example?
Thanks.

Let’s see what you did.

 db.test.insert({"sunnydays": [220, 232, 205, 211, 242, 270]})
 var pipeline = [
   {
     "$match": {
       "sunnydays": {
         "$gt": 211
       },
       "count": {
         "$sum": 1
       }
     }
   }
 ]

db.test.aggregation(pipeline)

I can’t see your attempt to add $sum in the $match stage.

But I do put "count" : { "$sum" : 1} within the “$match” block.

I meant, I can’t see your attempt to use $expr and $sum in the $match stage.

Just like it advises in the error message, $sum only works in a $project stage. If you want to use it in a $match stage, you need the $expr operator to make it work. Give it a try by looking at the $expr documentation.

I got it. Thank you :smiley:

:slightly_smiling_face: :+1: