Chapter 4: LAB 2 :Logic Operators

db.companies.find( {"$and":[ 
           {"$or":[{"category_code":"web"},         
                      {"category_code":"social"}]}, 
           {"$or":[{"founded_year":"2004"},         
                      {"founded_month":10}]} ]} ).count()

returns 76 as an answer which is wrong.

db.companies.find({ "$and": [
                        { "$or": [ { "founded_year": 2004 },
                                   { "founded_month": 10 } ] },
                        { "$or": [ { "category_code": "web" },
                                   { "category_code": "social" }]}]}).count()

if we reverse the OR condition as shown above, the result is 149(Correct Answer!).
How come reversing OR operation yields different result???

Hi @kesav_kumar, welcome to the community. :wave:
Please note that your first query expression finds documents with founded month "2004" as a string:

While your second query is finding documents with founded month 2004 as a number:

And since "2004" is not equal to 2004 because they have different data types, both the commands are giving different results.

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

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

3 Likes