MongoDB聚合(通过MongoEngine / PyMongo):我可以访问ReferenceField对象吗?

时间:2016-01-23 10:56:49

标签: mongodb pymongo mongoengine

假设我有两个类似的模型类:

class Institution(Model):
    name = StringField()

class Student(Model):
    full_name = StringField()
    institution = ReferenceField(Institution)

是否可以在汇总管道中访问Student.institution.name

1 个答案:

答案 0 :(得分:2)

来自monogdb版本3.2 这可以通过$lookup实现,但有一些限制(例如,不适用于分片集合),例如:

db.student.aggregate([
    {
      $lookup:
        {
          from: "institution",
          localField: "institution",
          foreignField: "_id",
          as: "institution"
        }
   }
])