假设我正在创建一个名为'评论'的模型,但评论模型的回复字段应该是'评论'类型。这可能吗?如果可以,请帮助我
const commentSchema = new mongoose.Schema({
comment: {
type: String,
required: true
},
author: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
reply: {
type: [Comments]
}
},
{
timestamps: true
})
const Comments = mongoose.model('Comment', commentSchema)
module.exports = Comments
你可以使用 this
以供自我参考。
const commentSchema = new mongoose.Schema({
comment: {
type: String,
required: true
},
author: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
reply: {
type: [this]
}
},
{
timestamps: true
})
const Comments = mongoose.model('Comment', commentSchema)
module.exports = Comments