MongoDB查找(JOIN),带有双嵌套数组中的字段
使用MongoDB集合名称部门
带有以下结构:
{
"_id":99,
"name":"Erick Kalewe",
"faculty":"Zazio",
"lecturers":[
{
"lecturerID":31,
"name":"Granny Kinton",
"email":"[email protected]",
"imparts":[
{
"groupID":70,
"codCourse":99
}
]
},
{
"lecturerID":36,
"name":"Michale Dahmel",
"email":"[email protected]",
"imparts":[
{
"groupID":100,
"codCourse":60
}
]
}
]
}
另一个集合组
带有此结构:
{
"_id":100,
"codCourse":11,
"language":"Romanian",
"max_students":196,
"students":[
{
"studentID":1
}
],
"classes":[
{
"date":datetime.datetime(2022, 5, 10, 4, 24, 19),
"cod_classroom":100
}
]
}
加入它们以获取以下内容:
{
"_id":99,
"name":"Erick Kalewe",
"faculty":"Zazio",
"lecturers":[
{
"lecturerID":31,
"name":"Granny Kinton",
"email":"[email protected]",
"imparts":[
{
"groupID":70,
"codCourse":99
}
]
},
{
"lecturerID":36,
"name":"Michale Dahmel",
"email":"[email protected]",
"imparts":[
{
"_id":100,
"codCourse":11,
"language":"Romanian",
"max_students":196,
"students":[
{
"studentID":1
}
],
"classes":[
{
"date":datetime.datetime(2022, 5, 10, 4, 24, 19),
"cod_classroom":100
}
]
}
]
}
]
}
目标是获取带有号码的报告由一位教授教授的学生。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查询
(我认为总体上加入字段不应该深入内部)
(对于休息参数,我先保留$)
我们还根据评论将学生总结,
(对于休息论点,我先保留$)
我们与该系的最高学生一起演讲
(MongoDB也可以比较文档)
IN/OUT )
Query
(i think in general joining fields shouldn't go deep inside)
(for rest argument i keep the $first)
we sum also the students based on the comment
(for rest arguments i keep the $first)
we take the lecture with the max students in the department
(mongodb can compare documents also)
Playmongo (you can put your mouse at the end of each stage to see
in/out
of that stage)现在我们已经了解了一个问题(根据您的其他问题),答案可以是) :
部门
记录其所有相关组的集合。$查找
仅为每个组创建groups
数组的学生ID。组
数据插入每个讲师
。的唯一学生
maximpartsStudents
,这是来自其所有groups
groups$ ydard
$ redion讲师
数组仅包括讲师
带有最高maximpartsstudents
。比结合两个问题的解决方案要好得多。
请查看其在
Now that we understand the question (according to your other question), an answer can be:
department
document a set of all its relevant groups.$lookup
only the student ids for each group to create agroups
array.groups
data to eachlecturer
.maxImpartsStudents
which is the number of unique students perlecturer
from all of itsgroups
$reduce
thelecturers
array to include only thelecturer
with highestmaxImpartsStudents
.Which is much better than combining the solutions from both questions.
See how it works on the playground example
You can try aggregation framework,
$lookup
withgroup
collection passlecturers.imparts.groupID
aslocalField
and pass <代码> _id ASforeffield
$ addfields
to mergegroup
groupimports
和removegroup
字段,因为不需要$ map
要迭代讲师
array> array$ mergeObject
以合并的当前对象讲师
和imports
$ map
的更新对象imports
array array$ mergeObjects
合并的当前对象导入
,并从group
$ filter
找到group> group
array的循环并通过groupId
$ arrayElemat
从上面过滤结果获取第一个元素You can try aggregation framework,
$lookup
withgroup
collection passlecturers.imparts.groupID
aslocalField
and pass_id
asforeignField
$addFields
to mergegroup
data withimports
and removegroup
fields because it is not needed$map
to iterate loop oflecturers
array$mergeObjects
to merge current object oflecturers
and updated object ofimports
$map
to iterate loop ofimports
array$mergeObjects
to merge current object ofimports
and found result fromgroup
$filter
to iterate loop ofgroup
array and find the group bygroupID
$arrayElemAt
to get first element from above filtered resultPlayground