亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用貓鼬發表評論回復模式

使用貓鼬發表評論回復模式

慕容森 2022-12-22 14:30:39
我想不出一種方法來創建適當的模式來處理帖子、評論和回復。主要停留在我將如何處理對評論的回復上?;旧暇拖?reddit,他們發帖然后回復,你可以回復每個人的回復視覺評論const CommentSchema = new mongoose.Schema({    username: {        type: String,        required: true,    },    detail: {        type: String,        required: true,    },})const PostSchema = new mongoose.Schema({    author: {        type: String,        required: true,    },    title: {        type: String,        required: true    },    description: {        type: String,        required: true    },    comments: [CommentSchema]})我制作的上述模式不處理對其他評論的回復。我還可以如何處理回復?
查看完整描述

1 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

如果您將評論 CommentSchema 硬合并到 Post Schema 中,您將始終受到硬編碼層數的限制。


相反,最好的做法是引用其他文檔而不合并它們。例如(這只是眾多方式中的一種):


comments: [CommentSchema]從 PostSchema 中刪除。


const CommentSchema = new mongoose.Schema({


    // always require all comments to point to the top post, for easy query of all comments whether nested or not

    postId: {

        type: ObjectId,

        ref: 'posts',

        required: true,

    }


    parentCommentId: {

        type: ObjectId,

        ref: 'comments',

        required: false, // if not populated, then its a top level comment

    }

 

    username: {

        type: String,

        required: true,

    },


    detail: {

        type: String,

        required: true,

    },


})

現在,當您加載帖子時,執行查詢以獲取所有評論postId: post._id并根據需要以圖形方式顯示它們。另一個可能的主要模式是,您不是從評論到帖子向上引用,而是從帖子到評論 [到評論等] 向下引用,這允許查找但不是那么簡單的簡單查詢。


祝你好運!


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號