Chapter 4: Is this correct?

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

is this correct ? I am getting 8 as an answer

I reformatted your query and it helps find out the issue.

{ "$or" :
  [
    { "$and" :
      [
        { "founded_year" : 2004 } ,
        { "$or" : [{"category_code":"web"},{"category_code":"social"}]} ,
        { "founded_month" : 10 } ,
        { "$or" : [{"category_code":"web"},{"category_code":"social"}]}
      ]
    }
  ]
}

So it looks like you are selecting founded_year AND founded_month rather than founded_year OR founded_month.

2 Likes