Why am I getting different results?

Why am I different results for:

> db.grades.find({"student_id": {"$gt": 25}, "student_id": {"$lt": 100}}).count()

`getting 1000`
> db.grades.find({"student_id": {"$gt": 25, "$lt": 100}}).count()

`getting 740`

Hey @vid_Proli.

Welcome to the MongoDB Community Forums!

Querying the above MQL gives all the documents matching the student_id greater than 25 + all the documents which are less than 100. So, it is returning 1000.

Whereas this query is returning all the documents which are greater than 25 but less than 100, which means documents lying between student_id 25 and 100. ( 25 < result_documents < 100). So, it is returning 740.

I hope it helps!

In case you have any doubts, please feel free to reach out to us.

Kushagra Kesav,
Curriculum Services Engineer

1 Like

Why? From I’ve learnt in the lecture

{"$and": [{"sector": "Mobile Food Vendor"}, {"result": "Warning"}]}

is equivalent to

{"sector": "Mobile Food Vendor", "result": "Warning"}

Is it because of the operators presence in the query?

Also, for the following code on sample_training db

db.zips.updateOne({"zip": "35014"}, {"$inc": {"pop": 10}})

if I omit {"zip": "35014"}(and change updateOne to updateMany), I would get an error

MongoInvalidArgumentError: Update document requires atomic operators

why? what is the proper way of writing this?
sorry for so many questions

Hey @vid_Proli,

To clarify more, what you are referring to in your first post is this :arrow_double_down:

I have modified your query using the $and operator:


db.grades.find({"$and":[{"student_id": {"$gt": 25}}, {"student_id": {"$lt": 100}}]}).count()

is similar to

db.grades.find({"student_id": {"$gt": 25, "$lt": 100}}).count()

Here both return 740. :arrow_heading_down:

For References: https://docs.mongodb.com/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-field


Hi @vid_Proli,

To use updateMany which means you want to update all the document’s pop field.

So, you will have to use empty {} (curly parenthesis) which depicts all the documents in that particular collection. Something like this:

db.zips.updateMany({}, {"$inc": {"pop": 10}})

which will return the number of modifiedCount.

I hope it helps!


Thanks alot for all the help :smiley:

1 Like

We wish you happy learning with the rest of the course!

In case you have any doubts, please feel free to reach out to us.

Kushagra Kesav,
Curriculum Services Engineer

1 Like

Sorry, I’m still having trouble understanding, how to shorten queries with $and operator.
so from M001: MongoDB Basics → chapter4 lecture query operators - Comparison
on sample_training db

>              "tripduration": {"$lte": 70}, 
>              "usertype": {"$ne": "Subscriber"}

is working as having a default $and operator, like this:

>db.trips.find({"$and": [
>                  {"tripduration": {"$lte": 70}}, 
>                  {"usertype": {"$ne": "Subscriber"}}]

but, when it comes to the question I mentioned above,

>               "student_id": {"$gt": 25},
>               "student_id": {"$lt": 100}

would produce different result comparing to having $and

>db.grades.find({"$and": [
>               {"student_id": {"$gt": 25}}, 
>               {"student_id": {"$lt": 100}}]

The only difference was in the grades query, I’m using the same field_name. Is it because the $and operator just work this way? like when having the same field, the comparison operators have to be in the same dictionary

>    		{"student_id": {"$gt": 25, "$lt": 100}}

or I misunderstood something?

See the following Request for explanation! -- Chapter 4: Query Operators Lecture - #12 by Stennie.

Basically when a key is repeated in json document, hence mql queries, only the last occurrence is kept.

Try the following in the shell to see the resulting document.

query = { "student_id": {"$gt": 25}, "student_id": {"$lt": 100} }

Thank you so much for your help, Appreciate :grinning:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.