【发布时间】:2020-03-28 12:25:34
【问题描述】:
我是 MongoDB 的新手。我对嵌套集合有一些问题。假设我在 mongodb 中有 3 个不同的集合。 (1) 课程 (2) 科目和 (3) 章节。
类包含不同的类名称,主题包含特定类的不同主题名称,章节包含特定主题的不同主题名称。
使用 mongoose,我可以获取/查找父级的章节集合,如下所示:
ChapterModel.find({})
.limit(1)
.populate([
{ path: "classId", model: ClassModel },
{ path: "subjectId", model: SubjectModel }
]);
给出以下结果:
[
{
"_id": "5de6a660a5f6a42060d532cd",
"name": "Physical Quantities and Their Measurements",
"subjectId": {
"_id": "5de6a660a5f6a42060d532c5",
"classId": "5de6a65fa5f6a42060d532c4",
"name": "Physics"
},
"classId": {
"_id": "5de6a65fa5f6a42060d532c4",
"name": "IX"
}
}
]
但是我找不到通过以下方式获取类子的任何方法:
[
{
"_id" : ObjectId("5de6a660a5f6a42060d532c5"),
"name": "IX",
"subjects": [
{
"_id" : ObjectId("5de6a660a5f6a42060d532c6"),
"name": "Physics",
"classId": ObjectId("5de6a660a5f6a42060d532c5"),
"chapters": [
{
"_id" : ObjectId('5c09fb04ff03a672a26fb23a'),
"name": Physical Quantities and Their Measurements",
"classId": ObjectId("5de6a660a5f6a42060d532c5"),
"subjectId": ObjectId("5de6a660a5f6a42060d532c6"),
},
{
"_id" : ObjectId("5de6a660a5f6a42060d532c6"),
"name": "Motion",
"classId": ObjectId("5de6a660a5f6a42060d532c5"),
"subjectId": ObjectId("5de6a660a5f6a42060d532c6"),
}
]
}
]
}
]
他们是否有类似填充的方式来查找所有嵌套的子项或集合?
【问题讨论】: