Chapter 3: Deeper Dive into the MongoDB Query Language Challenge Problem: Single Value in an Array of Integers

Hi! I just wanted to post my idea of the answer in this challenge that is a bit different.

First, I made a simple search to know if all the docs inside the collection had 3 elements, that was true because when I made a find search with $size and .count() on the mongo shell, it told me that it where the same exact number of elements of the collection.

(See the number of the result on the shell and behind in the compass)

Then I made this logic:

db.scores.find({$or: [{$and: [{“results.0”: {$gte: 70}}, {“results.0”: {$lt: 80}}]}, {$and: [{“results.1”: {$gte: 70}}, {“results.1”: {$lt: 80}}]}, {$and: [{“results.2”: {$gte: 70}}, {“results.2”: {$lt: 80}}]}]}).count()

This was the result:

The same that the challenge said was the correct answer :slight_smile:

Pd: I uploaded this because on the challenge said:


This problem is provided as a supplementary learning opportunity. It is somewhat more challenging that the ordinary labs. It is ungraded. We do not ask you submit an answer.

Hi @Edmundo_92011,

Thanks for sharing your solution.

Couple of things that I would like to mention :

  • How will you write the query if you have let’s say 100 elements in the array rather than just 3 ?
  • How will you write the query if the array has variable length ?

Maybe you can use $elemMatch operator here :slight_smile: .

Hope it helps!

Shubham Ranjan
Curriculum Services Engineer