Chapter 4 Lab 2: Querying Arrays and Sub-Documents

The query db.inspections.find({ “”: “NEW YORK” }).count() returns 18279 documents. However, there are another 5 documents with city: “New York”.
How do we include these 5 other documents in the query besides using a $or operator? Can the value be case-insensitive?
{ “$or”: [{“”: “NEW YORK”}, {“”: “New York”}] }

Hi Paul,

Option 1. I would do:

 db.inspections.find({ "": /New York/i }).count()

Or you can build a more specific regex..

(This is not covered in the course, but if you know some JS & regex then it’s not that complicated.)

Option 2. If it’s just a specific list of values, you can also use $in

 db.inspections.find({ "":{$in:["New York", "NEW YORK"]}}).count()

You can make it more readable:

const  newYorks = ["New York", "NEW YORK"]
 db.inspections.find({ "":{$in:newYorks}}).count()

You may also create an case insensitive index with the appropriate collation option.

Please refer to the following:


Hi @Paul_Tan,

I hope your doubts are clear now. Please let us know if you have any other questions.

~ Shubham