Chapter 4: Lab 2: Logic Operators

Am stuck on Chapter 4, Lab 2 on Logic Operators. Looking for assistance.

Lab is: How many companies in the sample_training.companies dataset were either founded in 2004 and either have the social category_code or web category_code, or were founded in the month of October and also either have the social category_code or web category_code?

Filter I tried was:
{"$and":[{"$or":[ {“founded_year”:2004},


Output count is 668 and wrong…

Just some ideas, rather than a solution. Re-state the problem:

  • founded in 2004
    • social category_code [or] web category_code,


  • founded in the month of October
    • social category_code [or] web category_code

Try to represent each logical bit, in pseudocode

$or:[ year 2004, month october]

Each one inside needs $and, as we wrote in bullets above, hence:

  $and:[ year 2004, $or: [social, web] ], 
  $and:[month october, $or:[social, web] ]

There is a lot to do yet, but this is a starting point.

1 Like

Thank you for the feedback. This logically makes sense. After breaking it down into smaller pieces I also realized the excess use of curly brackets was giving me incorrect results ie.



db.companies.find({“founded_year”:2004, category_code:“web”}).count()

The first is incorrect, the second format is correct.
So one cannot throw extra curly brackets in like they are mathematical parenthesis. Figured answer out after working through this. Thanks!

1 Like