如何从mongodb显示无限的嵌套注释

时间:2017-12-24 20:55:44

标签: node.js mongodb

因此每条评论都是自己的文件。当用户回复一个时,我是如何在彼此之间嵌套评论的? 如果嵌套的东西可以说回复多达10个深度,我怎么做一个for循环来循环遍历呢?

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});

1 个答案:

答案 0 :(得分: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级