Question: Chapter 4: Advanced CRUD Operations Lab 2: Logic Operators

Hi ! I have a question about the answer of this problem, when I use this code :

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

the result I get is 81
but with use the code below which is exactly the same, I get another answer

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

Can someone tell me where is the diffrence between them ? And why they don’t have the same answer ?

Hi :wave: @khaoula_Benaicha,

Welcome to MongoDB Community forums :sparkles:

You missed one extra space before founded_month which doesn’t match any field in the companies collection

:point_right: {" founded_month":10} due to which you got different output compared to this code:

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

Let us know if you have any further questions.

Thanks,
Kushagra

3 Likes

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