[英]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.