Mongoengine:匹配重音字符作为基础字符

时间:2020-04-13 14:33:01

标签: python string mongodb mongoengine

相关:How to do this in MongoDB db.foo.find() syntax

假设我有一个模型

class Foo(Document):
    name = StringField()

集合的数据库状态为:

[{"name":"Jesús"},{"name":"Jesus"}]

我想要一个与两个文档都匹配的查询,即搜索,但是规范了变音符号,例如:

Foo.objects.filter(name__icontains="jesus")

有没有一种方法可以直接在mongoengine中实现?

1 个答案:

答案 0 :(得分:0)

使用python,您可以import unidecode,然后将所有重音与普通文字进行比较,

from unidecode import unidecode
li = [ ]
for entry in Foo.objects:
    if unidecode(entry.name) == "Jesus":
        li.append(entry)

# now li has your filtered entries

或者,您可以

from unidecode import unidecode
li = [ entry for entry in Foo.objects if unidecode(entry.name) == "Jesus" ]

注意:您必须使用unidecode安装pip install unidecode

编辑:以下代码按预期工作,

>>> unidecode('Jesús')
Jesus
相关问题