[英]Setting up a complex comment model in NodeJs and mongoose
I am setting up a comment model where users can post comments reference
and can also reply.我正在设置评论 model,用户可以在其中发布评论
reference
,也可以回复。 the complication comes with the reply part.复杂之处在于回复部分。 I want users to be able to reply to comments or others' replies, and I am lost on how to set up my model for that.
我希望用户能够回复评论或其他人的回复,但我不知道如何为此设置我的 model。
How should I set up my model to be able to capture that data in my reply?我应该如何设置我的 model 以便能够在我的回复中捕获该数据?
also, any other suggestion would be appreciated此外,任何其他建议将不胜感激
Here is the model I am currently setting up这是我目前正在设置的 model
const mongoose = require('mongoose')
const commentSchema = new mongoose.Schema({
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
reference: {
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Project' || null,
default: false
},
body: {
type: String,
required: true,
trim: true
},
reply: {
owner: {
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'User'
},
body: {
type: String,
required: true
}
}
}, {
timestamps: true
})
const Comment = mongoose.model('Comment', commentSchema)
module.exports = Comment
If you are thinking about a model where we have如果您正在考虑我们有的 model
some post
>commentA
>replyA-a
>replyA-a-a
>replyA-a-a-a
>replyA-b
>commentB
>commentC
I would aggregate everything for the corresponding entity我会汇总相应实体的所有内容
Comment {
user,
body,
replies: [Comment] // pattern composite
}
EntityComment { // only persist this one
reference: { id, type: post|topic|whatever },
comment: [Comment]
}
Props are:道具是:
entityComment
can grow big (is this problematic?) entityComment
可以变大(这有问题吗?) If record entityComment
becomes too big (the max record length seems to be 16MB so likely not be the limit, but maybe the payload is slow to load), then如果记录
entityComment
变得太大(最大记录长度似乎是 16MB 所以可能不是限制,但可能有效负载加载速度很慢),然后
[{ ref: Comment, type: ObjectId)}]
)[{ ref: Comment, type: ObjectId)}]
)body: [ref: CommentBody, type: ObjectId]
)body: [ref: CommentBody, type: ObjectId]
) The reason is body
is likely the culprit (datasize wise), and this would allow to原因是
body
可能是罪魁祸首(数据大小明智),这将允许
entityComment
entityComment
中 There are tradeoffs:有权衡:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.