python - Mongo引擎查询引用字段

标签 python mongodb django-mongodb-engine

我已经定义了类声明:

from mongoengine import *
class TickDataDocument(Document):
"""
"""
    instrument_id = StringField(max_length=10, unique=True, required=True)
    tick_data = ReferenceField(TickDocument)


class TickDocument(Document):
"""
"""
    price = DecimalField(precision=2, required=True) 
    volume = LongField(required=True)  
    turnover = DecimalField(precision=2, required=True)  
    update_time = DateTimeField(unique=True, required=True)

我想查询某个时间段的update_time。

 TickDataDocument.objects(instrument_id="fa1100").filter(tick_data__ update_time__lt =datetime.datetime(2013,9,3))

但我得到错误:

mongoengine.errors.InvalidQueryError: Cannot perform join in mongoDB: tick_data__update_time

我怎样才能找到五种解决方案来查询 mongoengine 中的内部引用字段。

最佳答案

正如评论中所建议的,我分两步(两个查询)执行此操作:

tick_docs = list(TickDocument.object(update_time__lt)=datetime.datetime(2013,9,3))

TickDataDocument.objects(instrument_id="fa1100").filter(tick_data__in=tick_docs)

如果有更简单的解决方案,我也很感兴趣。

关于python - Mongo引擎查询引用字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32155962/

相关文章:

django - 当我将 View 包含在 django Mongoengine Web 服务 url.py 文件中时,为什么会出现 base_name 属性错误?

python - 导入错误 : No module named objectid

python - Pandas:对于排序系列 B 的所有元素,查找排序系列 A 中最接近元素的索引

python - Mongoengine-类型错误: Instantiating a document with positional arguments is not supported

mongodb - 如何让 Unity3D 2018(或更高版本)与 MongoDB C# 驱动程序一起使用?

mongodb - 如何在 MongoDB 中创建用户

python - '~'(波浪号)运算符在Python中的应用

python - 在空数据帧上调用 parse_date

python - 更改同一字典中的键值: Python

django - 使用 django 和 mongoengine 时出现 NotUniqueError