gpt4 book ai didi

python - Mongoengine - 使用 icontains 与所有

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:27 26 4
gpt4 key购买 nike

我见过this question但它并没有回答我的问题,甚至没有很好地提出它。

我认为最好用一个例子来解释这一点:

class Blah(Document):
someList = ListField(StringField())

Blah.drop_collection()

Blah(someList=['lop', 'glob', 'hat']).save()
Blah(someList=['hello', 'kitty']).save()

# One of these should match the first entry
print(Blah.objects(someList__icontains__all=['Lo']).count())
print(Blah.objects(someList__all__icontains=['Lo']).count())

我假设这会打印 1, 00, 1 (或奇迹般地 1, 1),但它却给出

0
Traceback (most recent call last):
File "metst.py", line 14, in <module>
print(Blah.objects(someList__all__icontains=['lO']).count())
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py", line 1034, in count
return self._cursor.count(with_limit_and_skip=True)
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py", line 608, in _cursor
self._cursor_obj = self._collection.find(self._query,
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py", line 390, in _query
self._mongo_query = self._query_obj.to_query(self._document)
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py", line 213, in to_query
query = query.accept(QueryCompilerVisitor(document))
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py", line 278, in accept
return visitor.visit_query(self)
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py", line 170, in visit_query
return QuerySet._transform_query(self.document, **query.query)
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py", line 755, in _transform_query
value = field.prepare_query_value(op, value)
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/fields.py", line 594, in prepare_query_value
return self.field.prepare_query_value(op, value)
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/site-packages/mongoengine/fields.py", line 95, in prepare_query_value
value = re.escape(value)
File "/home/blah/.pythonbrew/pythons/Python-3.1.4/lib/python3.1/re.py", line 246, in escape
return bytes(s)
TypeError: 'str' object cannot be interpreted as an integer

这两个查询都不起作用!

MongoEngine是否支持使用icontainsall进行搜索的某种方式?或者有什么办法可以解决这个问题?

注意:我想使用 MongoEngine,而不是 PyMongo。

编辑:Python 2.7.3 也存在同样的问题。

最佳答案

截至目前(版本 0.8.0),执行此操作的唯一方法是使用 __raw__ 查询,可能与 re.compile() 结合使用。就像这样:

import re

input_list = ['Lo']
converted_list = [re.compile(q, re.I) for q in input_list]

print(Blah.objects(__raw__={"someList": {"$all": converted_list}}).count())
<小时/>

目前 mongoengine 中还没有办法将 allicontains 结合起来,唯一可以与其他运算符一起使用的运算符是 notthe docs巧妙地提到了这一点。 ,正如其中所说:

not – negate a standard check, may be used before other operators (e.g. Q(age_not_mod=5))

强调我的

但它并没有说你不能用其他运算符来做到这一点,实际上就是这样。

<小时/>

您可以通过查看源代码来确认此行为:

版本 0.8.0+(in module - mongoengine/queryset/transform.py - 第 42-48 行):

if parts[-1] in MATCH_OPERATORS:
op = parts.pop()

negate = False
if parts[-1] == 'not':
parts.pop()
negate = True

在旧版本中,可以在 _transform_query 方法中的 mongoengine/queryset.py 中看到上述行。

关于python - Mongoengine - 使用 icontains 与所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15958459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com