2017-12-24 15 views
1

からINFINITELYネストされたコメントを表示する方法。各コメントはそれ自身のドキュメントです。これは、ユーザーが1つに返信したときに、お互いにコメントをネストする方法ですか? 何か入れ子になっていると、返信で10の深さまで言えますが、どのようにforループを実行すればいいのですか?mongodb

var RK = mongoose.Schema.ObjectId; 
var CommentSchema = Schema({ 
    body: {type: String}, 
    chapterId: {type: RK, ref: 'Chapter'}, 
    by: {type: RK, ref: 'User'}, 
    children: [{ 
     type: RK, 
     ref: 'Comment' 

    }] 
}, {timestamps: true}); 
+0

TBHこのノー良い答えのようなものに関するかなりの数の質問がありますように思えるカントー。まともなものを思いつくために最善を尽くしてください。 – kappaflow

答えて

0
let dependencySchema = mongoose.Schema({ 
    dependencyComment: { 
     type: mongoose.Schema.Types.ObjectId, 
     required: false 
    }, 
    dependencyAuthor: { 
     type: mongoose.Schema.Types.ObjectId, 
     required: false 
    } 
}); 

let CommentSchema = mongoose.Schema({ 
    //Rest of you schema 
    children: [{ 
     type: mongoose.Schema.Types.ObjectId, 
     ref: [dependencySchema] 

    }] 
}, {timestamps: true}); 

この方法をネストN番目のレベルにコメント