mongoose - 使用 mongoose.findOne 中的文档获取子文档
问题描述
首先,我的代码运行良好,但我不知道是否做对了。我有一个具有此架构的父模型单元
{
info: {...},
chapters: {_id: string, ...}
}
章节架构是这样的:
{
info: {...},
content: string
}
现在在客户端,如果我正在编辑章节,并且想将更改放入数据库,我会这样做
Units.findOne({'chapters._id': chapter_id}) //this query will return the parent unit
.then(unit=>{ //is there a way to have the chapter with unit here??
const chapter = unit.chapters.find(chapter=> chapter._id === chapter_id)
//seems like duplicated. i already found the unit based on the chapter_id
//after that i update the chapter
})
就是这样,我希望有一种方法可以在第一个查询中返回章节
解决方案
推荐阅读
- c++ - win10如何调试崩溃?
- javascript - 数组中有值,但无法访问它们,可能是什么问题?
- javascript - 如何让我的数组从 firebase 获得与我的静态数组完全相同的输出?
- function - 如何使用不重置每个增量的变量创建整数函数
- c - Bufferoverflow,snprintf 而不是 char 调整大小?
- java - 许多线程正在等待条件和服务器的负载超过 100
- php - Symfony 3.4、MoneyType 和 Bootstrap
- amazon-web-services - 有没有办法使用 boto3 中的集群名称检查 emr 集群状态?
- swift - 如何快速创建固定大小的二维数组
- python-3.x - 我正在使用 Pytest 检查我的更新功能,我没有做错什么?