1

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

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

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, 0or 0, 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 是否支持某种方式来使用icontainsand进行搜索all?或者有什么办法可以解决这个问题?

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

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

4

1 回答 1

1

截至目前(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中没有办法将alland组合起来icontains,唯一可以和其他算子一起使用的算子是not. 文档中巧妙地提到了这一点,因为它说:

not - 否定标准检查,可以在其他运算符之前使用(例如 Q(age_ not _mod=5))

强调我的

但它并不是说你不能用其他运营商做到这一点,事实就是如此。


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

版本 0.8.0+(在模块中 - 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

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

于 2013-04-28T07:57:47.560 回答