# Challenge Problem: Single Value in an Array of Integers... "at least one score" or "all the scores"

Since spanish is my mother’s tongue probably I got lost in translation:
“How many documents contain at least one score in the results array that is greater than or equal to 70 and less than 80?”*

*“At least one score” … meaning one, two or three out of them, no matter their position into the array has to complay with the "greater than or equal to 70 and less than 80" demand… so when I try no solved this at first I try this:

db.scores.find({“results”: {\$gte: 70, \$lt: 80}}).count()
1020

Then I try this one:

db.scores.find({“results”: {\$elemMatch: {\$gte: 70, \$lt: 80}}}).count()
744

But looking at the results I realized that each of the score comply with the "greater than or equal to 70 and less than 80"

So just to make clear and to be sure that I understood the \$elemMatch: command would you please clarify this issue to me

1 Like

Hi @Jose_20145,

Your first query will return the count of all such documents which has elements greater than or equal to 70 and less than 80. It does not have to be the same element.

For example, let’s insert three documents in our collection named col.

``````db.col.insertMany([
{"title":"A", "values":[60,90,30]},
{"title":"B", "values":[80,90,47]},
{"title":"C", "values":[75,90,30]}
])``````

Now, we want to return only those documents which has at least one element greater than or equal to 70 and less than 80.

``db.col.find({"values":{\$gte:70,\$lt:80}})``

Now, as we can see in the output of this query, it has returned all the documents because all the documents has at least one element which is greater than or equal to 70 and less than 80. However, it does not have to be the same element.

``{"title":"B", "values":[80,90,47]}``

In this case, we have 47 which is smaller than 80 and we have two elements (80, 90) which is greater than or equal to 70. So, it satisfies the criteria.

The `\$elemMatch` operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

``db.col.find({"values":{\$elemMatch:{\$gte:70,\$lt:80}}})``

As, we can see in the output of this query, there is only one element in the returned document (i.e. 75) which satisfies both the criteria.

If you still have any query, please feel free to get back to us.

Happy Learning

Thanks,
Shubham Ranjan
Curriculum Support Engineer

2 Likes