1. Return model data filtered
I want to add some data into a mongoose model before we return it to front-end, what should I do?
// Mongoose Model:
const BookSchema = new Schema({
  title: {
    type: String,
    require: true
  },
  author: {
    type: String
  }
});
Book = mongoose.model('Book', BookSchema);
If we want to perform a mongoose query with Book and try added other data (e.g book comments) into the query result, the comments would not return to front-end as desired.
// storedBook: a model returned by mongoose query.
// comments: a mode returned by mongoose query.
storedBook.comments = comments;
console.log(storedBook.comments); // comments could found here.
res.json(storedBook); // but front-end could not get comments.
It seems the comments are ignored when return to front-end. Is the reason that there is no matched field named comments in Book model? I've noticed that sometimes we extract the data we want to return from a model instead of directly return the model. Or add some command when perform query using a '-' in front of a field we do not want to select: select('-_id -oneFieldNameThatYouDoNotWantToSelect'). Then, I try to convert mongoose document model into an plain object by using model method .toObject(), and add other data as new property to this object. Mongoose Models, which inherit from Documents.
const book = storedBook.toObject();
book.comments = comments.toObject();
console.log(book.comments);
res.json(book); // now, the book contains comments!
In some cases, we could use embedded document or reference document. The above discussion is just a found. Reference