簡體   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