Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive search with $in

Tags:

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 ?

like image 605
Onkar Janwa Avatar asked May 22 '12 11:05

Onkar Janwa


1 Answers

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"]    } ] 
like image 191
Zaur Nasibov Avatar answered Oct 12 '22 02:10

Zaur Nasibov