我正在尝试为“ comment”类型创建一个Mongoose模式,该模式在“ text”,“ time”,“ user”字段内,但也包含“ comments”数组,这意味着该评论的答案。如何指定注释中该字段的数组类型也是注释。我为您留下了一些我想做的代码。替代方案也很受欢迎(除了将答案的ID存储在数组中之外,这是一个很粗糙的解决方案)。非常感谢
const CommentSchema = new Schema({
text: { type: String, required: false },
timestamp: { type: Date, required: true, default: Date.now },
user: { type: String, required: true },
answer: [ CommentSchema.schema ],
liked: [ String ]
});
答案 0 :(得分:0)
您可以试试吗?我认为应该可以
const CommentSchema = new Schema({
text: { type: String, required: false },
timestamp: { type: Date, required: true, default: Date.now },
user: { type: String, required: true },
answer: {
type: [CommentSchema],
default: []
},
liked: [ String ]
});
答案 1 :(得分:0)
您尝试这种打击模式(或)并阅读docs。
const answersCommentsSchema = new Schema({
text: { type: String, default : null },
user: { type: String, required: true },
},{timestamps : true});
const CommentSchema = new Schema({
text: { type: String, default : null },
user: { type: String, required: true },
answer: [answersCommentsSchema],
liked: {type : Array, default:[]}
},{timestamps : true});