Chapter 4 - Lab 2 - why does short form $or does not work?

Hi there,
I’ve successfully completed the Lab 2, but I always try to understand why some things work and other things don’t.

Specifically in this case, this query is correct:
db.companies.find({ "$and": [{ "$or": [{"category_code": "social"}, {"category_code": "web"}]}, { "$or": [{"founded_year" : 2004}, {"founded_month" : 10}]} ]})

However this one does not work:
db.companies.find({ "$and": [{ "$or": {"category_code": {"web","social"}}}, { "$or": [{"founded_year" : 2004}, {"founded_month" : 10}]} ]})

The only difference is I tried using shorthand for the first $or, since it is applied to the same field. My logic was if there is an implicit $and in this query:

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

I assumed we could use the same short form notation while specifying the $or operator.

Am I wrong?

Hi @Samuel_Leith

You can’t use short form notation for $or.

This is not a valid JSON. Please note that all MongoDB queries are JSON.

$or operator must be an array. The syntax is wrong. Please refer our documentation for more information.

Hope it helps!

~ Shubham