2016-01-23 114 views
0

假設我有兩個模型類,像這樣:MongoDB聚合(通過MongoEngine/PyMongo):我可以訪問ReferenceField對象嗎?

class Institution(Model): 
    name = StringField() 

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

是否有可能訪問Student.institution.name在聚合管道?

+0

你能告訴你的查詢?如果你問的是如何從'Student'對象獲得'name',那麼是的!你可以做's.institution.name' – styvane

回答

1

從monogdb 3.2版這是可能的,但$lookup有一些限制(例如,不能在分片收集工作),例如:

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