Python Mongoengine-将icontains与所有
我看到了,但它没有回答我的问题,甚至没有很好地提出它 我认为最好用一个例子来解释这一点:Python Mongoengine-将icontains与所有,python,mongoengine,Python,Mongoengine,我看到了,但它没有回答我的问题,甚至没有很好地提出它 我认为最好用一个例子来解释这一点: 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
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,0
或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
0
回溯(最近一次呼叫最后一次):
文件“metst.py”,第14行,在
打印(Blah.objects(someList\uu all\uu icontains=['lO']).count())
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site-packages/mongoengine/queryset.py”,第1034行,以计数
返回self.\u cursor.count(带\u limit\u和\u skip=True)
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/queryset.py”,第608行,在光标处
self.\u cursor\u obj=self.\u collection.find(self.\u query,
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/queryset.py”,第390行,在查询中
self.\u mongo\u query=self.\u query\u obj.to\u query(self.\u文档)
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/queryset.py”,第213行,在to_查询中
query=query.accept(QueryCompilerVisitor(文档))
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/queryset.py”,第278行,在accept中
回访访客。访问查询(自我)
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/queryset.py”,第170行,访问查询
返回QuerySet.\u转换\u查询(self.document,**query.query)
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/queryset.py”,第755行,在转换查询中
值=字段。准备\u查询\u值(op,值)
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/fields.py”,第594行,在prepare\u query\u值中
返回self.field.prepare\u query\u值(op,value)
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/site packages/mongoengine/fields.py”,第95行,在prepare\u query\u值中
值=重新转义(值)
文件“/home/blah/.pythonbrew/pythons/pythons-3.1.4/lib/python3.1/re.py”,第246行,在escape中
返回字节
TypeError:“str”对象不能解释为整数
这两个查询都不起作用
MongoEngine是否支持使用icontains
和all
进行搜索?或者是否支持绕过此问题的方法
注意:我想使用MongoEngine,而不是PyMongo
编辑:Python 2.7.3也存在同样的问题。到目前为止(版本0.8.0)实现这一点的唯一方法是使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
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中没有办法将all
和icontains
组合在一起,唯一可以与其他操作符一起使用的操作符是not
。这在中被巧妙地提到,如中所述:
not–否定标准检查,可在其他操作员之前使用(例如Q(年龄\非\模=5))
重点矿山
但它并不是说你不能与其他运营商合作,事实上就是这样
您可以通过查看源来确认此行为:
版本0.8.0+(-42-48行):
在较早的版本中,上述行可以在mongoengine/queryset.py中的\u transform\u query
方法中看到。到目前为止(版本0.8.0)实现这一点的唯一方法是使用\u raw\u
查询,可能与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中没有办法将all
和icontains
组合在一起,唯一可以与其他操作符一起使用的操作符是not
。这在中被巧妙地提到,如中所述:
not–否定标准检查,可在其他操作员之前使用(例如Q(年龄\非\模=5))
重点矿山
但它并不是说你不能与其他运营商合作,事实上就是这样
您可以通过查看源来确认此行为:
版本0.8.0+(-42-48行):
在旧版本中,上述行可以在\u transform\u query
方法中的mongoengine/queryset.py中看到。如果进行原始查询,如Blah.objects({someList:{“$all”:[“/10/i”]})。count()
?使用它返回计数的0
,就像我描述的第一个打印选项一样。很抱歉,我使用了0
而不是O
,请尝试以下操作:Blah.objects({uu raw}{“someList”:{“$all”:[“/1O/I”]})。count()
:)别担心,我已经做了更正。它不起作用。我将把原来的帖子改为Lo
,以消除混淆。也许这是mongoDB的问题,而不是mongoengine的问题。我现在没有时间检查,但可能以后。你最终为此做了什么?这里有同样的问题,如果你做一个原始查询,比如Blah.objects({uu raw}{“someList”){“$all”:[/10/i]}.count()
?使用它返回0
作为计数,就像我描述的第一个打印选项一样。对不起,我使用了0
而不是O
,试试这个:Blah.objects({uu raw={“someList”){:[“/1O/i”]}}.count()
:)别担心,我已经改正了。它不起作用。我将把原来的帖子改为Lo
,以消除混淆。也许这是mongoDB的问题,而不是mongoengine的问题。我现在没有时间检查,但可能以后。你最终为此做了什么?我也有同样的问题