2016-03-10 130 views
0
var schema = new mongoose.Schema({ 
name: { type: String, require: true }, 
comments: [{ 
    id: {type: Schema.Types.ObjectId, ref: 'Comment'}, 
    vote: Number, 
}], 
creator: {type: Schema.Types.ObjectId, ref: 'User'}, 

我可以填充creator,但不能填充comment如何用其他字段在貓鼬中填充子文檔?

Posts 
.findOne({ id: id }) 
.populate('creator') 
.populate('comments') 
.exec(function (err, post) { 
}); 

回答

1

請嘗試這一個,與comments.id如下

Posts 
    .findOne({ id: id }) 
    .populate('creator') 
    .populate('comments.id') 
    .exec(function (err, post) { 
    });