Chapter 3 Challenge Problem - array values displaying 80 and above too

I am getting the count 744. I am seeing array values which are 80 and above too as below.
Please advise, as elemMatch should the exact match.

Thank You

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.scores.find({results: {$elemMatch: {$gte: 70, $lt: 80}}}).count();
744

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.scores.find({results: {$elemMatch: {$gte: 70, $lt: 80}}}).pretty();
{
“_id” : ObjectId(“5964e8e5f0df64e7bc2d737c”),
“results” : [
72,
77,
80
]
}
{
“_id” : ObjectId(“5964e8e5f0df64e7bc2d7390”),
“results” : [
72,
77,
80
]
}
{
“_id” : ObjectId(“5964e8e5f0df64e7bc2d7377”),
“results” : [
72,
84,
80
]
}
{
“_id” : ObjectId(“5964e8e5f0df64e7bc2d7375”),
“results” : [
78,
99,
87
]
}
{
“_id” : ObjectId(“5964e8e5f0df64e7bc2d7387”),
“results” : [
72,
77,
80
]

Hi.
The MongoDb documentation states that :

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

Since the challenge asks you to find documents with at least one score >= 70 and < 80, that means you solved it! Yeah! \o/

Now if you’d like to go further and find documents with all of their scores matching, this problem was discussed and solved in this topic.

.

Even I have the same doubt . I found one reply to this and I did not understand the explaination. Can somebody explain me why the final result set is showing value greater than 80 also

Repeating what was written

The import part is at least one element. In the example where 99 is present, 78 and 87 are also present. This means you have 2 elements that matche the criteria. Having 2 means you have at least one.

Hi @Archana_83391,

db.scores.find({results: {$elemMatch: {$gte: 70, $lt: 80}}}).pretty();

When you run this query, you are basically asking the mongodb database to return all those documents which has at lease one element in the results array field which falls in the range {$gte: 70, $lt: 80}.

Let’s say this is an array field in our document

"results":[698, 342, 6, 77, 1000]

The document corresponding to this array field will be selected because there is at least one value in this array field i.e. 77 which satisfies the filter criteria.

For additional reading you can refer our documentation and other posts in our forum :

$elemMatch
Similar post

Hope it helps!

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

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Now I got it. Thank you very much sir .