2014-03-14 174 views
1

有人可以幫助我與這種模式的人口?我需要通過他們的userId填充員工數組。貓鼬填充 - 數組

var PlaceSchema = new Schema ({ 
    name:  { type: String, required: true, trim: true }, 
    permalink: { type: String }, 
    country: { type: String, required: true }, 
     ...long story :D... 
    staff:  [staffSchema], 
    admins:  [adminSchema], 
    masterPlace:{ type: Boolean }, 
    images:  [] 

}); 

var staffSchema = new Schema ({ 
    userId: { type: Schema.Types.ObjectId, ref: 'Account' }, 
    role: { type: Number } 
}); 

var adminSchema = new Schema ({ 
    userId: { type: Schema.Types.ObjectId, ref: 'Account'} 
}) 

var Places = mongoose.model('Places', PlaceSchema); 

我試過使用這個查詢,但沒有成功。

Places.findOne({'_id' : placeId}).populate('staff.userId').exec(function(err, doc){ 
    console.log(doc); 
}); 

回答

0

Polpulation旨在爲的方法從集合中的相關模型信息「拉動」。因此,而不是「直接」指定相關領域,而不是參考相關的字段,以便文檔出現有所有嵌入在響應這些子文件的:

Places.findOne({'_id' : placeId}).populate('staff','_id') 
    .exec(function(err, doc){ 
    console.log(doc); 
}); 

第二個參數只是返回你想要的字段。所以它「過濾」了迴應。

在文檔中有關於populate的更多信息。