Implementation of nodejs Project comment function

encountered a problem when trying to tamper with a forum with express+mongoose to implement the comment function.

model: of the post

const ArticleSchema = new Schema({
  author: { type: Schema.Types.ObjectId, ref: "User" },
  createTime: { type: Date },
  thumb: [{ type: Schema.Types.ObjectId, ref: "User" }],
  comment: [{ type: Schema.Types.ObjectId, ref: "Comment" }],
  text: String
});

model: of comments

const CommentSchema = new Schema({
  text: String,
  createTime: { type: Date },
  poster: { type: Schema.Types.ObjectId, ref: "User" },
  posts: {type: Schema.Types.ObjectId, ref: "Article"}
})

render data:

  Article.find()
    .sort("-createTime")
    .populate("author")
    .populate("comment")
    .exec((err, articles) => {
      if (err) return next(err)
      res.render("container/index", {
        title: "xxxx",
        articles
      })
    })

Front end:

:comments[i].text
: comments[i].poster

can get the content of the comment, but the reviewer displays the value of _ id, in which case should I query User again?

Mar.14,2021

deep-populate

User.
  findOne({ name: 'Val' }).
  populate({
    path: 'friends',
    // Get friends of friends - populate the 'friends' array for every friend
    populate: { path: 'friends' }
  });

use the aggregate query .aggregate , or you can check it again.

Menu