- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我见过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, 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
这两个查询都不起作用!
MongoEngine是否支持使用icontains
和all
进行搜索的某种方式?或者有什么办法可以解决这个问题?
注意:我想使用 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 中还没有办法将 all
和 icontains
结合起来,唯一可以与其他运算符一起使用的运算符是 not
。 the 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/
Mongoengine、flask-MongoEngine 和 Django-MongoEngine 项目之间有什么区别? 我在 Django 项目中使用 Mongoengine。如果我改用 Djan
如果我有这样的文档: class Page(Document): title = StringField(max_length=200, required=True) date_mod
我有以下类(class) class ParentDocument(Document): . . . class Child1Document(ParentDocument):
我一直对这个错误感到困惑。我正在使用 mongoengine 创建一个 Vehicle 数据库,就像这样。 class Vehicles(Document): make: StringFiel
我一直对这个错误感到困惑。我正在使用 mongoengine 创建一个 Vehicle 数据库,就像这样。 class Vehicles(Document): make: StringFiel
需要你的帮助。我尝试使用 mongoengine、flask、flask-login。 我的模型: class Post(db.Document): text = db.StringField(
django mongoengine和mongoengine有什么区别我可以使用 django 默认/内置形式 mongodb 最佳答案 Django-mongoengine 是一个提供与 Mongo
一个 mongoengine.DynamicEmbeddedDocument可用于利用 MongoDB 灵活的无模式设计。它是可扩展的,并且不对字段应用类型约束,afaik。 一个 mongoengi
无论我尝试什么,当尝试在 mongoengine 数据库上使用多处理时,我都会不断点击“MongoClient opened before fork”警告关于不 fork 事件的 mongo 连接。
我想使用mongoengine和flask定义两个文档,用户包含以下主题,主题包含发布用户。 我在user.py中定义User文档,在topic.py中定义Topic文档。仅在 topic.py 中我
我有一个使用 Flask+MongoEngine 创建的应用程序,并且有一个像这样的“用户文档”: class User(db.Document): username = db.StringF
我正在使用 mongoengine v0.15.0。如何获取连接到的数据库的名称?当然,我会在 uri 字符串中提供名称。但是,有没有办法查询 mongo 并找到它? 谢谢, 戒日 最佳答案 有关在
使用Mongoengine并尝试形成标签云。我想在每个项目上附加一个或多个标签。此处使用类似标签的类似内容(在每个问题下方)。 在这里搜索和阅读许多帖子之后,我仍然无法通过正确的方法将新条目添加到Li
我正在为我最新的 Web 应用程序使用 Flask/Mongoengine-MongoDB。 我熟悉 Pymongo,但我是对象文档映射器(如 Mongoengine)的新手。 我已经建立了一个数据库
我有一个动态文档模型,我要设置的字段之一称为“index_this” 我正在尝试使用点表示法设置此字段: In [17]: mongo_object.index_this = False In [18
我正在使用 mongodb 和 python。 同样使用MongoEngine与mongodb进行通信。 现在我做了一个简单的具有评论功能的看板系统。 [模型.py] import datetime
为了在 mongoengine 上进行涉及更新两种以上不同类型文档的事务,我该怎么做? 最佳答案 使用下面的方法,就可以使用mongoengine中的事务请注意,在此方法中,您的查询必须是使用 pym
我在一些 mongoengine 的文档中有一个所需权限的列表,我想对其进行过滤。 考虑这个文档: class Entry(Document): required_perms = ListFi
我有一个看起来像这样的模型: class TestDoc(Document): name = StringField() friends = ListField(StringField
我想将动态字段存储到文档中,但每个文档可以有不同的字段。 例如: Class SampleDoc(DynamicDocument): xyz = StringField() df = "fiel
我是一名优秀的程序员,十分优秀!