繁体   English   中英

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

[英]python mongodb regex: ignore case

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

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"}})

参考: http//docs.mongodb.org/v2.2/reference/operator/regex/

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'}})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM