I have this document structure in my collection:
{
"grades": [
{ "grade": "A", "score": 8 },
{"grade": "A", "score": 7 }
]
}
I need to filter for those documents when their score is divided by 7 has 0 as a reminder. Original question is asked here (question 30). Their solution is simple:
db.restaurants.find(
{"grades.score" :
{$mod : [7,0]}
},
{"restaurant_id" : 1,"name":1,"grades":1}
);
However, in order to practice, I wanted to to my own solution, which doesn't work out.
db.restaurants.aggregate([
{
"$match": {
"$expr": {"$in": [0, [ { $mod: [ "$grades.score", 7 ] } ] ]}
}
},
])
The error is:
PlanExecutor error during aggregation :: caused by :: $mod only supports numeric types, not array and int
Basically I want to check if 0 is in custom made array which will contain only single value after modulo operation, as explained in the docs for $in when used in aggregation.
Is there a way to make $mod work as part of aggregation?
I see that similar question is asked here, but it is only referencing first score in grades array not all of them. So in my case I should see example document given on the beginning, since 7 % 7 = 0
{ "grades.score" : { $mod : [7,0] } }is better since you don't have to actually write any loop logic or current value steps, etc. My answers below were wrt "I wanted to to my own solution".