在 python mongoengine 中通过计算属性查询

发布于 2025-01-13 08:41:03 字数 1116 浏览 1 评论 0原文

我想知道是否可以使用 python 中的 mongoengine 通过计算属性来查询 MongoDB 中的文档。

目前,我的模型如下所示:

class SnapshotIndicatorKeyValue(db.Document):
    meta = {"collection": "snapshot_indicator_key_values"}

    snapshot_id = db.ObjectIdField(nullable=False)
    indicator_key_id = db.ObjectIdField(nullable=False)
    value = db.FloatField(nullable=False)
    created_at = db.DateTimeField()
    updated_at = db.DateTimeField()

    @property
    def snapshot(self):
        return Snapshot.objects(id=self.snapshot_id).first()

    def indicator_key(self):
        return IndicatorKey.objects(id=self.indicator_key_id).first()

当我执行 SnapshotIndicatorKeyValue .objects().first().snapshot 操作时,我可以访问 snapshot 属性。

但是当我尝试查询它时,它不起作用。例如:

SnapshotIndicatorKeyValue.objects(snapshot__date_time__lte=current_date_time)

我收到错误“mongoengine.errors.InvalidQueryError:无法解析字段“快照”``

有什么方法可以使其与查询一起使用吗? 我需要根据snapshot 的属性查询SnapshotIndicatorKeyValue

I wondered if it is possible to query documents in MongoDB by computed properties using mongoengine in python.

Currently, my model looks like this:

class SnapshotIndicatorKeyValue(db.Document):
    meta = {"collection": "snapshot_indicator_key_values"}

    snapshot_id = db.ObjectIdField(nullable=False)
    indicator_key_id = db.ObjectIdField(nullable=False)
    value = db.FloatField(nullable=False)
    created_at = db.DateTimeField()
    updated_at = db.DateTimeField()

    @property
    def snapshot(self):
        return Snapshot.objects(id=self.snapshot_id).first()

    def indicator_key(self):
        return IndicatorKey.objects(id=self.indicator_key_id).first()

When I do for example SnapshotIndicatorKeyValue .objects().first().snapshot, I can access the snapshotproperty.

But when I try to query it, it doesn't work. For example:

SnapshotIndicatorKeyValue.objects(snapshot__date_time__lte=current_date_time)

I get the error `mongoengine.errors.InvalidQueryError: Cannot resolve field "snapshot"``

Is there any way to get this working with queries?
I need to query SnapshotIndicatorKeyValue based on a property of snapshot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2025-01-20 08:41:03

为了直接通过 mongoengine 查询 snapshot 属性,您可以在 SnapshotIndicatorKeyValue< 中引用相关的 snapshot 对象而不是 snapshot_id /code> 文档定义。

使用 参考字段 修改后的模型将如下所示:

from mongoengine import Document, ReferenceField

class Snapshot(Document)
property_abc = RelevantPropertyHere() # anything you need

class SnapshotIndicatorKeyValue(Document):
snapshot = ReferenceField(Snapshot)

您将连续保存 Snapshot 实例和 SnapshotIndicatorKeyValue 实例,如下所示:

sample_snapshot = Snapshot(property_abc=relevant_value_here) # anything you need
sample_snapshot.save()

sample_indicatorkeyvalue = SnapshotIndicatorKeyValue()
sample_indicatorkeyvalue.snapshot = sample_snapshot
sample_indicatorkeyvalue.save()

然后您可以引用 snapshot 的任何属性 通过:

SnapshotIndicatorKeyValue.objects.first().snapshot.property_abc

In order to query the snapshot property directly through mongoengine, you can reference the related snapshot object rather than the snapshot_id in your SnapshotIndicatorKeyValue document definition.

An amended model using a Reference field would be like this:

from mongoengine import Document, ReferenceField

class Snapshot(Document)
property_abc = RelevantPropertyHere() # anything you need

class SnapshotIndicatorKeyValue(Document):
snapshot = ReferenceField(Snapshot)

You would sucessively save an instance of Snapshot and an instance of SnapshotIndicatorKeyValue like this:

sample_snapshot = Snapshot(property_abc=relevant_value_here) # anything you need
sample_snapshot.save()

sample_indicatorkeyvalue = SnapshotIndicatorKeyValue()
sample_indicatorkeyvalue.snapshot = sample_snapshot
sample_indicatorkeyvalue.save()

You can then refer to any of the snapshot's properties through:

SnapshotIndicatorKeyValue.objects.first().snapshot.property_abc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文