# Chapter 3: [SOLVED] Challenge Problem | Variation

Hi everyone!

I’m so excited taking this course.

I got a little question. What do we have to do if the question were: How many documents contain ALL the scores in the results array that are greater than or equal to 70 and less than 80?

I mean, the output should be for instance: `results [70, 75, 79]`

How do we build the query? Thanks a lot for!

one solution would be to use and operator and specify all array positions, so first, second and third position in an array.

something like this (I adapted query to return some results):
{\$and: [{“results.0”:{\$gte:70,\$lt:80}},{“results.1”:{\$gte:87,\$lt:90}},{“results.2”:{\$gte:88,\$lt:90}}]}

1 Like

Hi dalejodc,

You are absolutlely right. We can easily use the logical operators here if it was not an array.

Use `\$elemMatch` operator to achieve the desired results. Here is the detailed explanation: \$elemMatch (query)

Kanika

1 Like

Yes, that could be an option. But in that case, we must know the size of the array.

The size of the array is not needed for using \$elemMatch. It says at least one element, matching the condition. \$elemMatch will be checked against each element of the array and if at least one matches that document matches the condition

Hi @Jesus_Javier_55985, thanks a lot for replying!

Yes, you are completely right. As you said: “The size of the array is not needed for using \$elemMatch. It says at least one element, matching the condition.

But in this case, I was experimenting a variation for the problem. I was looking for match every single element in the array, not “at least one” item.

Regards!

I see what you mean now. Well, I have thought about it and an alternative would be to use negative logic, I mean, look for a document not matching the opposite expression. Let’s explain a bit better , look for all document that don´t have any element matching the condition and then negate it. But I haven´t been able to find the proper combination, for example I can not use {results:{\$not:{\$elemMatch:{\$lt:70,\$gte:80}}}} because no element can be at the same time lees than and greater than, other alternative is removing \$elemMatch, but it doesn´t work either , because in that case we look for array that
has at least one element doesn not match.

I will try to think a bit more, if you find something by following negation logic, please let me know

Of course @Jesus_Javier_55985.

Definitely, we have to think a little bit more about this Challenge Problem | Variation. If I found some solution, I’ll share it here.

Regards!

Hi @dalejodc,

You can also take a look here: [Solution] Challenge Problem Solved

Kanika

@dalejodc

I have found a solution using \$where operator. With other operators I haven´t managed to success
I have used 60 and 90 as values because it seems that there is not any element with all elements between 70 and 80

db.scores.find( { \$where: function() {
return this.results.reduce(function(result, element) {
return result && (element >= 60 && element < 90)
}, true);
} } );

1 Like

This thread is for a variation of the problem. The idea is to find a document with all values within the range. The solution to the challenge for for at least one

Nice @Jesus_Javier_55985!

``````db.scores.find( { \$where: function() {
return this.results.reduce(function(result, element) {
return result &amp;&amp; (element &gt;= 60 &amp;&amp; element &lt; 90)
}, true);
} } );
``````

That is exactly what we were looking for. A query where every single element match in the array with the query, not just one item.

If someone else is reading this solution, remember that is a variation for the problem.

For instance, in this case the results are >=70 && results <= 80, and all the item of the array match the query.

Again, thanks a lot @Jesus_Javier_55985.
Regards!

{\$or: [{“results.0”: {\$gte: 70, \$lt: 80}},{“results.1”: {\$gte: 70, \$lt: 80}},{“results.2”: {\$gte: 70, \$lt: 80}}]}

or use the elemMatch!

Please, have a read at Query an Array, you will find a solution simpler than the one you depicted.