[英]Setting up a complex comment model in NodeJs and mongoose
我正在设置评论 model,用户可以在其中发布评论reference
,也可以回复。 复杂之处在于回复部分。 我希望用户能够回复评论或其他人的回复,但我不知道如何为此设置我的 model。
我应该如何设置我的 model 以便能够在我的回复中捕获该数据?
此外,任何其他建议将不胜感激
这是我目前正在设置的 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
如果您正在考虑我们有的 model
some post
>commentA
>replyA-a
>replyA-a-a
>replyA-a-a-a
>replyA-b
>commentB
>commentC
我会汇总相应实体的所有内容
Comment {
user,
body,
replies: [Comment] // pattern composite
}
EntityComment { // only persist this one
reference: { id, type: post|topic|whatever },
comment: [Comment]
}
道具是:
entityComment
可以变大(这有问题吗?) 如果记录entityComment
变得太大(最大记录长度似乎是 16MB 所以可能不是限制,但可能有效负载加载速度很慢),然后
[{ ref: Comment, type: ObjectId)}]
)body: [ref: CommentBody, type: ObjectId]
) 原因是body
可能是罪魁祸首(数据大小明智),这将允许
entityComment
中有权衡:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.