R
R
riddlr2018-10-10 19:07:04
MongoDB
riddlr, 2018-10-10 19:07:04

How to add comments to comments?

It is not entirely clear how the data is arranged in this case. An example can be found on reddit - https://www.reddit.com/r/news/comments/9my7g1/cana...
In addition to comments on the post itself, branches of responses to specific comments are created. How to organize it in terms of data model?
For example, I have Post and Comment models

const postSchema =  new mongoose.Schema ({
  author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  slug: {
    type: String,
    unique: true
  },
  title: {
    type: String
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
  contributors: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]
})

const commentSchema =  new mongoose.Schema ({
  author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  text: {
    type: String
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' }
})

What about nested comments?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question