2015-04-01 70 views
1

说我有收藏/文件象下面这样:可能填充两个级别?

问题集:

{ 
    _id: ObjectId("0000"), 
    title: "test question", 
    survey: ObjectId("1234") //abbreviated for question assume this is a legit object id 
} 

调查收集:

{ 
    _id: ObjectId("1234"), 
    title: "survey title!", 
    user: ObjectId("5678") 
} 

用户采集:

{ 
    _id: ObjectId("5678"), 
    Name: "Abe" 
} 

有我的方式来调用类似:

questions.findOne({_id: "0000"}).populate("survey.user") 

与问题相处的用户信息?我知道我可以填写question的直系父母,但我很好奇,如果可以为祖父母做。

回答

1

您需要分两步进行;首先填充survey,然后填充survey.user使用单独的呼叫Model.populate

questions.findOne({_id: '0000'}) 
    .populate('survey') 
    .exec(function(err, question) { 
     questions.populate(
      question, 
      { path: 'survey.user', model: 'User'}, 
      function(err, question) {...}); 
    });