One More Challenge

Following was the challenge problem provided to us at the end of chapter 3.

How many documents contain at least one score in the results array that is greater than or equal to 70 and less than 80?

After changing the problem slightly I am finding it really difficult to find the answer. I want to find at least 2 scores; rest all remains same. I tried to combine $elemMatch with $size but for me it is not working … Help on this would be highly appreciated…

Below is the one I wrote but for some reason it is NOT working.

{$and:[{“results”:{$elemMatch: {$gte:70, $lt:80}}},{“results”:$size:{$gte:1}}]}

The problem statement can be broken down thus:
Find…

  • Results >= 70 and < 80

Count…

  • Number of documents that meet criteria 1

$size counts the number of elements in an array so it won’t suffice.

Please read my complete question. I want to find documents with at least 2 elements meeting the condition of >=70 and <80

In that case you need to use the aggregation pipeline aggregate() with the $filter() operator. However, I don’t know if the aggregation pipeline was lightly discussed in this course.