Case Insensitive search with $in

mongodb, nosql

Solution

You can use $elemMatch with regular expressions search, e.g. let's search for "blue" color in the following collection:

db.items.save({ 
  name : 'a toy', 
  colors : ['red', 'BLUE'] 
})
> ok
db.items.find({
  'colors': {
    $elemMatch: { 
      $regex: 'blue', 
      $options: 'i' 
    }
  }
})
>[ 
  {   
    "name": "someitem",
    "_id": { "$oid": "4fbb7809cc93742e0d073aef"},   
    "colors": ["red", "BLUE"]
   }
]

Problem

How to search a column in a collection in mongodb with `$in` which includes an array of elements for search and also `caseInsensitive` matching of those elements in the column ?

Original source

Related problems