mongoengine 内嵌文档查询
我使用的是 flask-mongoengine.
查询内嵌文档
Topic.objects(userinfo__username='HGB')
这样查询内嵌文档是没有问题的是可以查到结果的。
我有一个数据模型中的内嵌文档使用了一个 GenericEmbeddedDocumentField
代码如下:
information = db.GenericEmbeddedDocumentField(required=True, choices=[AnimeInformation, ComicInformation])
class AnimeInformation(db.EmbeddedDocument):
episode = db.StringField()
subtitle = db.StringField()
is_finished = db.BooleanField(default=False)
publish_date = db.DateTimeField(default=datetime.utcnow())
updated_at = db.DateTimeField(default=datetime.utcnow())
Topic.objects(information__episode='12')
理论上使用上面的这句话查询是没错的呀。
但是会报错。
错误:
mongoengine.errors.InvalidQueryError
mongoengine.errors.InvalidQueryError: Cannot resolve subfield or operator episode on the field information
Traceback (most recent call last)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Volumes/DATA One/dev/workspaces/BadLoli-Flask/api/topics.py", line 40, in index
information__episode='12'
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/base.py", line 299, in first
result = queryset[0]
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/base.py", line 197, in __getitem__
queryset._cursor[key],
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/base.py", line 1547, in _cursor
self._cursor_obj = self._collection.find(self._query,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/base.py", line 1590, in _query
self._mongo_query = self._query_obj.to_query(self._document)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/visitor.py", line 89, in to_query
query = query.accept(QueryCompilerVisitor(document))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/visitor.py", line 155, in accept
return visitor.visit_query(self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/visitor.py", line 78, in visit_query
return transform.query(self.document, **query.query)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mongoengine/queryset/transform.py", line 63, in query
raise InvalidQueryError(e)
mongoengine.errors.InvalidQueryError: Cannot resolve subfield or operator episode on the field information
相关文档 http://docs.mongoengine.org/a...
一头雾水
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到问题了 mongoengine bug
我使用的是 flask-mongoengine 1.11版本 / mongoengine 版本是0.15
我把mongoengine 升级到 0.15.3。就好了。好了。。。
以上就没出现问题了 正常查询到结果。