python mongodb regex: ignore case
How can I specify a REGEX and ignore the case:
regex = ".*" + filter + ".*";
config.gThingCollection.find({"name":{开发者_StackOverflow中文版"$regex":regex}})
I want the filter to be case-insensitive, how to achieve that?
Try using the python regex objects instead. Pymongo will serialize them properly:
import re
config.gThingCollection.find({"name": re.compile(regex, re.IGNORECASE)})
You can use MongoDB regex options in your query.
config.gThingCollection.find({"name":{"$regex":regex, "$options": "-i"}})
res = posts.find({"geography": {"$regex": 'europe', "$options": 'i'}})
# if you need use $not
import re
res = posts.find(
{"geography": {"$not": re.compile('europe'), "$options": 'i'}})
Your code should look like this:
regex = ".*" + filter + ".*";
config.gThingCollection.find({"name":{"$regex":regex, "$options":"i"}})
Reference: http://docs.mongodb.org/v2.2/reference/operator/regex/
精彩评论