开发者

python mongodb regex: ignore case

开发者 https://www.devze.com 2023-02-09 21:45 出处:网络
How can I specify a REGEX and ignore the case: regex = \".*\" + filter + \".*\"; config.gThingCollection.find({\"name\":{开发者_StackOverflow中文版\"$regex\":regex}})

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号