Sato
Sato

Reputation: 8622

How to populate subdocument with other fields in 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'},

I can populate creator, but cannot populate comment

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

Upvotes: 0

Views: 273

Answers (1)

zangw
zangw

Reputation: 48566

Please try this one, with comments.id as below

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

Upvotes: 2

Related Questions