如何用mongoose中的其他字段填充子文档?

时间:2016-03-10 04:09:29

标签: node.js mongoose

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 个答案:

答案 0 :(得分:2)

请尝试使用此comments.id,如下所示

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