2016-11-07 128 views
2

道歉,如果這已被問及,我的搜索沒有出現相同的情況。我有兩個架構喜歡的東西下面:貓鼬填充子文檔數組

var experimentSchema = new mongoose.Schema({ 

    name : 'string' 
    elements : [{ 
     type : mongoose.Schema.ObjectId, 
     ref: 'Element' 
    }], 
    resources : [{ 
     type : mongoose.Schema.ObjectId, 
     ref : 'Resource' 
    }], 

}) 


var elementSchema = new mongoose.Schema({ 
    name : 'string', 
    component : { 
     type : mongoose.Schema.ObjectId, 
     ref : 'Component' 
    } 
}) 

我想,這樣當我要求一個實驗,我得到一個物體的elementsresources一個數組,每個元素領域component的進行了深刻的人口也已經人口稠密。

我已經試過沿線的幾件事情:

Experiment.findOne(query).populate(['resources','elements','elements.component']).exec(...) 

沒有成功。任何人都可以爲這種類型的操作提供正確的語法嗎?

在此先感謝!

回答

1

希望這有助於。

models.User.findOne(query) 
     .populate([{ 
     path: 'elements', 
     populate: { 
      path: 'components', 
      model: 'Component' 
      } 
     },{ 
     path:'resources' 
     }]) 
     .exec(...)