简体   繁体   English

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

[英]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:道具是:

  • an entityComment can grow big (is this problematic?) entityComment可以变大(这有问题吗?)
  • no need for multiple fetch, everything's there无需多次获取,一切都在那里
  • easy to "hide" some comments and just show its count (array length)易于“隐藏”一些评论并仅显示其计数(数组长度)

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

  1. we can think of saving each comment (using replies: [{ ref: Comment, type: ObjectId)}] )我们可以考虑保存每条评论(使用回复: [{ ref: Comment, type: ObjectId)}]
  2. but maybe a better idea is to use a reference for body ( body: [ref: CommentBody, type: ObjectId] )但也许更好的主意是使用 body 的引用( body: [ref: CommentBody, type: ObjectId]

The reason is body is likely the culprit (datasize wise), and this would allow to原因是body可能是罪魁祸首(数据大小明智),这将允许

  • keep everything nested in entityComment将所有内容都嵌套在entityComment
  • delay the fetch of the bodies we are interested in (not the whole hierarchy)延迟获取我们感兴趣的主体(不是整个层次结构)

There are tradeoffs:有权衡:

  1. is fine for read适合阅读
  2. is simpler for writes (just update/delete a singular comment)写入更简单(只需更新/删除单个评论)

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

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