Potential Error In The Detailed Answer of Lab 1: Logic Operators (Chapter 4)

So, in this lab we are required to find all documents where the pop field is more than 1,000,000 and less than 5,000 in the sample_training.zips dataset.

Following are the two queries mentioned in the detailed answer for this lab.

  1. db.zips.find({ "pop": { "$gt": 5000, "$lt": 1000000 }}).count()

  2. db.zips.find({ "$nor": [ { "pop": { "$lt":5000 } }, { "pop": { "$gt": 1000000 } } ] } ).count()

But both queries are not equivalent, the first query checks if the population is greater than 5,000 and less than 1,000,000. Therefore it doesn’t include documents where population is equal to 5,000 or 1,000,000. So, it excludes the endpoints.

The second query on the other hand checks if the population is neither less than 5,000 nor greater than 1,000,000, which means the population is greater than equal to 5,000 and less than equal to 1,000,000. So, it includes the endpoints.

But if we test these queries the results would turn out to be identical because there’s no data where the population is equal to either 5,000 or 1,000,000, which can be verified using this query
{$or: [{pop: 5000}, {pop: 1000000}]}.
So, if we add a datapoint where the pop field equal to either 5,000 or 1,000,000 we would see a difference in counts returned.

Screenshot from the course.

Please let me know if I’m thinking in the right direction.

Hello @Som_Shekhar_Mukherjee

You’re right, if you’d like to check with exact endpoints, 1st query could look like:
db.zips.find({ "pop": { "$gte": 5000, "$lte": 1000000 } } ).count()
where $gte is described as greater than or equal and $lte - less than or equal

However given the case and known results, since both of values - 5.000 and 1.000.000 - are not present in the field, it makes no difference to the lab mechanism since it’s all about the result number, not the syntax itself.

Also “greater than OR equal” (and less than or equal) is checking both conditions, without skipping either of operation - that means if a value is greater than X, the other comparison (equality) is still performed as well.

Alternatively
{$and: [ {"pop":{$gt:5000}} , {"pop":{$lt:1000000} } ]}
which stands from 5.000 to 1.000.000 (without endpoints)