Popiulate sub docs in mongoose

31 Views Asked by At

I'm having trouble with a save on subdocument.

Im calling save method on parent model but it only saves once. When theres one subdocument it does not save another one.

const reply = await Comment
    .save({
      _id: commentID,
      answers: [{
        author: author,
        content: content
      }]
    })
1

There are 1 best solutions below

0
abagh0703 On

I assume that your schema for answers is the Schema.Types.Mixed. If you modify these, you need to use markModified to tell Mongoose that you changed it (this only applies to Mixed, not String or anything else like that).

//change your doc...
Comment.markModified('answers');
Comment.save({...})