gpt4 book ai didi

node.js - 如何显示来自 mongodb 的无限嵌套注释

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:04 25 4
gpt4 key购买 nike

所以每条评论都是它自己的文档。当用户回复评论时,这就是我将评论嵌套在一起的方式吗?如果嵌套了某些内容(假设回复深度达到 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});

最佳答案

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 层

关于node.js - 如何显示来自 mongodb 的无限嵌套注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47963947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com