![](/img/trans.png)
[英]How do you set a schema property to be of type SubDocument in Mongoose?
[英]How to push to mongoose schema property of type []
我在后端使用貓鼬,想知道設置這樣的類型數組的模式屬性是否正確?:
comments: {
type: [],
required: false,
}
然后推送到具有相同屬性的文檔,如下所示?:
thread.comments.push({
commenter: req.user.username,
content: comment,
});
thread.save();
由於評論是您的線程架構的子級,我建議使用SubDocuments :
const commentSchema = new Schema({
commenter: 'string',
content: 'string'
});
const threadSchema = new Schema({
comments: [commentSchema],
//...
});
添加評論:
thread.comments.push({
commentor: req.user.username,
content: comment //the text of the comment
});
thread.save();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.