我如何可以指定一个正则表达式,并忽略大小写:蟒蛇MongoDB的正则表达式:忽略大小写
regex = ".*" + filter + ".*";
config.gThingCollection.find({"name":{"$regex":regex}})
我希望过滤器不区分大小写,如何实现这一目标?
我如何可以指定一个正则表达式,并忽略大小写:蟒蛇MongoDB的正则表达式:忽略大小写
regex = ".*" + filter + ".*";
config.gThingCollection.find({"name":{"$regex":regex}})
我希望过滤器不区分大小写,如何实现这一目标?
请尝试使用python正则表达式对象。 Pymongo会正确地序列化它们:
import re
config.gThingCollection.find({"name": re.compile(regex, re.IGNORECASE)})
您可以在查询中使用MongoDB正则表达式选项。
config.gThingCollection.find({"name":{"$regex":regex, "$options": "-i"}})
您的代码应该是这样的:
regex = ".*" + filter + ".*";
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'}})