python mongodb regex:忽略cas

2024-04-18 23:32:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

regex = ".*" + filter + ".*";
config.gThingCollection.find({"name":{"$regex":regex}})

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


Tags: nameconfig过滤器findfilterregex区分gthingcollection
3条回答

您可以在查询中使用MongoDB regex选项。

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/

尝试改用python正则表达式对象。Pymongo将正确序列化它们:

import re
config.gThingCollection.find({"name": re.compile(regex, re.IGNORECASE)})

相关问题 更多 >