Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python mongodb regex: ignore case

How can I specify a REGEX and ignore the case:

regex = ".*" + filter + ".*";
config.gThingCollection.find({"name":{"$regex":regex}})

I want the filter to be case-insensitive, how to achieve that?

like image 868
Bin Chen Avatar asked Feb 12 '11 04:02

Bin Chen


2 Answers

Try using the python regex objects instead. Pymongo will serialize them properly:

import re
config.gThingCollection.find({"name": re.compile(regex, re.IGNORECASE)})
like image 90
Brendan W. McAdams Avatar answered Sep 30 '22 02:09

Brendan W. McAdams


You can use MongoDB regex options in your query.

config.gThingCollection.find({"name":{"$regex":regex, "$options": "-i"}})
like image 41
Ida N Avatar answered Sep 30 '22 00:09

Ida N