javascript - 如何推送到 [] 类型的猫鼬模式属性
问题描述
我在后端使用猫鼬,想知道这样设置数组类型的模式属性是否正确?:
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();
推荐阅读
- python - Surprise API - 如何将特征加载到 Surprise 数据集中?
- markdown - 如何在 svelte kit 中导入 markdown 文件?
- ios - 如何检查文本字段在 Swift 中是否有多个字母?
- reactjs - getInitialProps 在我的实时站点上导致 ERR_TOO_MANY_REDIRECTS 错误,但在我的本地版本上没有
- generative-adversarial-network - 判别器的真假损失是一样的,不变的
- asp.net-core - 避免在 ASP.NET Core 属性路由中使用魔法字符串
- android - 使用cordova-plugin-firebasex时如何连接到firebase本地模拟器
- c++ - 如何在 c++17 或 20 中使用 std::invoke_result_t 而不是 c++14 中的 std::result_of_t?
- c++ - std::lock_guard 是否在使用 std::adopt_lock 选项构造后释放互斥锁?
- java - 如何在 Spring Boot 中从命令行解析链接?