1

拥有这 2 个 MongoEngine 文档:

class A(Document):
    a = StringField()

class B(Document):
    b = StringField()
    boolfield = BooleanField(default=False)
    ref = ReferenceField(A)

我想首先filter()在一个特定的 A 对象上,然后从第一个查询开始,filter()在 BooleanField 上。但是这些行会导致错误:

a_objects = A.objects(a='test') # OK
query = B.objects(ref__in=a_objects) # OK
query2 = query.filter(boolfield=True) # FAILS

错误是:

TypeError: 'Collection' object is not callable. If you meant to call the '__deepcopy__' method on a 'Collection' object it is failing because no such method exists.

在此处查看完整代码和回溯:https ://gist.github.com/nferrari/4962245

谢谢!

4

1 回答 1

1

似乎不能在 0.7.8 中链接查询参考字段 - 所以暂时请使用字典,然后作为 kwargs 作为工作回合传入,例如:

    a_objects = A.objects(a='test')
    query_dict = {'ref__in': a_objects}
    query_dict['boolfield'] = True
    self.assertEquals(B.objects(**query_dict).count(), 1)

我已添加:https ://github.com/MongoEngine/mongoengine/issues/234将在 0.8 中修复

于 2013-02-20T14:36:44.130 回答