Chapter 4 Lab 2: Querying Arrays and Sub-Documents

The query db.inspections.find({ “address.city”: “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”: [{“address.city”: “NEW YORK”}, {“address.city”: “New York”}] }

1 Like

Hi Paul,

Option 1. I would do:

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

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({ "address.city":{$in:["New York", "NEW YORK"]}}).count()
 18284

You can make it more readable:

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

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

Please refer to the following:

2 Likes

Hi @Paul_Tan,

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

~ Shubham