使用MongoEngine查询ReferenceFields

Phi*_*ent 9 django reference mongodb

我正在玩MongoEngine,我找不到查询ReferenceFields的方式

Class Foo(Document)
    bar = ReferenceField(Bar)
    ...

Class Bar(Document)
    value =IntField()
    ...

bars = Bar.objects.filter(value__lt=1000)
Run Code Online (Sandbox Code Playgroud)

Django的:

foos = Foo.objects.filter(bar__in=bars)
Run Code Online (Sandbox Code Playgroud)

MongoEngine :?

有没有办法实现这个目标?

提前致谢,

Ank*_*haa 9

一个查询是不可能的.

试试这个:

bars = Bar.objects.filter(value__lt = 1000)
foo = Foo.objects.filter(bar__in = bars)
Run Code Online (Sandbox Code Playgroud)

更多看.那个测试脚本.
https://github.com/Ankhbayar/mongoengine/blob/dev/tests/django_tests.py#L73


And*_*ich 0

如果您使用引用,则无法查询引用的对象字段。因为引用是在驱动程序内部完成的,并且大多数驱动程序都会保存引用的文档 ID、集合名称和数据库名称(因此您只能查询引用的文档 ID)。

想要查询吗?使用嵌入或进行两个单独的查询。