Hi everybody, I am making a social networking application. But I’m a little confused about the schema. Which of these is a more professional structure? Or what build would you recommend? Is it better to create a separate model for the comment or specify it as an array in the Post?
example 1:
const CommentSchema = new mongoose.Schema(
{
commenter: {
type: mongoose.Types.ObjectId,
ref: "user",
required: true,
},
post: {
type: mongoose.Types.ObjectId,
ref: "post",
required: true,
},
content: {
type: String,
required: true,
},
parent: {
type: mongoose.Types.ObjectId,
ref: "comment",
},
children: [
{
type: mongoose.Types.ObjectId,
ref: "comment",
},
],
edited: {
type: Boolean,
default: false,
},
},
{ timestamps: true }
);
example 2
const commentSchema = new mongoose.Schema({
text: {
type: String,
required: true,
},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Post',
required: true,
},
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
replies: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment',
},
],
edited: {
type: Boolean,
default: false,
},
editedAt: {
type: Date,
},
createdAt: {
type: Date,
default: Date.now,
},
});