繁体   English   中英

在 NodeJs 和 mongoose 中设置复杂的注释 model

[英]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 所以可能不是限制,但可能有效负载加载速度很慢),然后

  1. 我们可以考虑保存每条评论(使用回复: [{ ref: Comment, type: ObjectId)}]
  2. 但也许更好的主意是使用 body 的引用( body: [ref: CommentBody, type: ObjectId]

原因是body可能是罪魁祸首(数据大小明智),这将允许

  • 将所有内容都嵌套在entityComment
  • 延迟获取我们感兴趣的主体(不是整个层次结构)

有权衡:

  1. 适合阅读
  2. 写入更简单(只需更新/删除单个评论)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM