Chapter 4 - Lab1 - Issues

The lab:

How many zips in the sample_training.zips dataset are neither over-populated nor under-populated?

In this case, we consider population of more than 1,000,000 to be over- populated and less than 5,000 to be under-populated.

I came up with two solutions that I thought did the same thing:

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

The problem is that these two queries return different results.

The first one returns 11193 and the second one returns 11194.

I submitted the value 11193, got the correct answer and then I looked the answers provided by MongoDB to see where the problem was, but the query using $nor was the same as mine.

I don’t see why these two queries return different results. Can anybody explain to me, please?

Hi, I run the commands and got 11193 for both.

Maybe a document was inserted after running the first command?


Another way to get the same number is

db.zips.count({pop:{$gt:5000}})

Since there are no elements larger than 100000 :slight_smile: .

(just for fun, this isn’t rigurous at all).

Hi @LucasXavier,

I hope your doubts are resolved now. You saw different count because most likely the data in your cluster got modified by some query that you ran earlier.

If you want, you can delete all the data in your cluster and reload the fresh data.

Let us know if you still have any confusions.

~ Shubham

Actually, the data was not modified. I ran both commands many times and the results were always the same

But you mentioned earlier that you are getting different results?

I mean, the results were always 11193 for the first query and 11194 for the second query, no matter how many times I ran both commands.
No data inside the database was modified between the two different queries

Could you try with itcount() rather than count()?

This is strange. Let me look into it.