2014-03-14 171 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的更多信息。