Mongoengine:基于重音符作为基础字符

问题描述 投票:0回答:1

相关: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中直接做到这一点?

python string mongodb mongoengine
1个回答
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

EDIT:以下代码按预期工作,

>>> unidecode('Jesús') Jesus
© www.soinside.com 2019 - 2024. All rights reserved.