python mongodb正则表达式:忽略大小写

Bin*_*hen 15 python regex mongodb

如何指定REGEX并忽略大小写:

regex = ".*" + filter + ".*";
config.gThingCollection.find({"name":{"$regex":regex}})
Run Code Online (Sandbox Code Playgroud)

我希望过滤器不区分大小写,如何实现?

Bre*_*ams 29

请尝试使用python正则表达式对象.Pymongo会正确地序列化它们:

import re
config.gThingCollection.find({"name": re.compile(regex, re.IGNORECASE)})
Run Code Online (Sandbox Code Playgroud)


Ida*_*a N 19

您可以在查询中使用MongoDB正则表达式选项.

config.gThingCollection.find({"name":{"$regex":regex, "$options": "-i"}})
Run Code Online (Sandbox Code Playgroud)