department使用具有以下結構的 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
}
]
}
]
}
group和另一個具有這種結構的集合:
{
"_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
}
]
}
]
}
]
}
目標是獲得一份報告,其中包含一個系教授教授的學生人數。
uj5u.com熱心網友回復:
詢問
- 放松,加入,然后重新組合
- 它有點大的查詢,因為你想加入嵌套欄位,這意味著 2 展開和 2 分組來恢復結構
(我認為一般來說加入欄位不應該深入內部) - 展開兩個陣列
- 在 groupID 上進行查找
- 現在將檔案構造為 2 級嵌套
- 首先它的影響需要被分組和推動(對于其余的論點,我保留$first)我們還根據評論對學生進行總結
- 然后是我需要分組和推送的講師(對于其余的論點,我保留$first)我們與系里的最大學生一起上課(mongodb也可以比較檔案)
Playmongo(您可以將滑鼠放在每個階段的末尾以查看in/out該階段)
department.aggregate(
[{"$unwind": "$lecturers"}, {"$unwind": "$lecturers.imparts"},
{"$lookup":
{"from": "coll",
"localField": "lecturers.imparts.groupID",
"foreignField": "_id",
"as": "lecturers.imparts"}},
{"$set": {"lecturers.imparts": {"$first": "$lecturers.imparts"}}},
{"$group":
{"_id": {"_id": "$_id", "lecturersID": "$lecturers.lecturerID"},
"name": {"$first": "$name"},
"faculty": {"$first": "$faculty"},
"lecturers":
{"$first":
{"lecturerID": "$lecturers.lecturerID",
"name": "$lecturers.name",
"email": "$lecturers.email"}},
"imparts": {"$push": "$lecturers.imparts"},
"lecture_max_students":
{"$sum": "$lecturers.imparts.max_students"}}},
{"$set":
{"lecturers":
{"$mergeObjects":
["$lecturers", {"imparts": "$imparts"},
{"lecture_max_students": "$lecture_max_students"}]},
"imparts": "$$REMOVE","lecture_max_students": "$$REMOVE"}},
{"$group":
{"_id": "$_id._id",
"name": {"$first": "$name"},
"faculty": {"$first": "$faculty"},
"lectures": {"$push": "$lecturers"},
"dept-max-lecturer":
{"$max": {"max-students": "$lecturers.lecture_max_students",
"lecturerID": "$lecturers.lecturerID"}}}}])
uj5u.com熱心網友回復:
你可以試試聚合框架,
$lookupwithgroupcollection passlecturers.imparts.groupIDaslocalField和 pass_idasforeignField$addFields合并group資料imports并洗掉group欄位,因為它不是必需的$maplecturers迭代陣列的回圈$mergeObjects合并當前物件lecturers和更新物件imports$mapimports迭代陣列的回圈$mergeObjects合并當前物件imports并從中找到結果group$filter迭代陣列回圈group并找到組groupID$arrayElemAt從上面的過濾結果中獲取第一個元素
db.department.aggregate([
{
$lookup: {
from: "group",
localField: "lecturers.imparts.groupID",
foreignField: "_id",
as: "group"
}
},
{
$addFields: {
lecturers: {
$map: {
input: "$lecturers",
in: {
$mergeObjects: [
"$$this",
{
imparts: {
$map: {
input: "$$this.imparts",
as: "i",
in: {
$mergeObjects: [
"$$i",
{
$arrayElemAt: [
{
$filter: {
input: "$group",
cond: { $eq: ["$$this._id", "$$i.groupID"] }
}
},
0
]
}
]
}
}
}
}
]
}
}
},
group: "$$REMOVE"
}
}
])
操場
uj5u.com熱心網友回復:
現在我們理解了這個問題(根據你的另一個問題),答案可以是:
- 為每個
department檔案添加一組所有相關組。 $lookup只有每個組的學生 id 才能創建一個groups陣列。- 將相關
groups資料插入到每個lecturer. - 計算哪個是每個來自其所有的
maxImpartsStudents唯一學生的數量lecturergroups $reducelecturers僅lecturer包含最高的陣列maxImpartsStudents。- 格式化答案
db.department.aggregate([
{
$addFields: {
groups: {
$setIntersection: [
{
$reduce: {
input: "$lecturers.imparts.groupID",
initialValue: [],
in: {$concatArrays: ["$$value", "$$this"]}
}
}
]
}
}
},
{
$lookup: {
from: "group",
let: {groupIDs: "$groups"},
pipeline: [
{$match: {$expr: {$in: ["$_id", "$$groupIDs"]}}},
{
$project: {
students: {
$reduce: {
input: "$students",
initialValue: [],
in: {$concatArrays: ["$$value", ["$$this.studentID"]]}
}
}
}
}
],
as: "groups"
}
},
{
$project: {
name: 1,
lecturers: {
$map: {
input: "$lecturers",
in: {
$mergeObjects: [
{lecturerID: "$$this.lecturerID"},
{groups: {
$map: {
input: "$$this.imparts",
in: {
$arrayElemAt: [
"$groups",
{$indexOfArray: ["$groups._id", "$$this.groupID"]}
]
}
}
}
}
]
}
}
}
}
},
{
$project: {
name: 1,
lecturers: {
$map: {
input: "$lecturers",
as: "item",
in: {
$mergeObjects: [
{
maxImpartsStudents: {
$size: {
$reduce: {
input: "$$item.groups",
initialValue: [],
in: {$setUnion: ["$$value", "$$this.students"]}
}
}
}
},
{lecturerID: "$$item.lecturerID"}
]
}
}
}
}
},
{
$set: {
lecturers: {
$reduce: {
input: "$lecturers",
initialValue: {
"maxImpartsStudents": 0
},
in: {
$cond: [
{$gte: ["$$this.maxImpartsStudents", "$$value.maxImpartsStudents"]},
"$$this", "$$value"
]
}
}
}
}
},
{
$project: {
lecturerID: "$lecturers.lecturerID",
maxImpartsStudents: "$lecturers.maxImpartsStudents",
departmentName: "$name"
}
}
])
這比結合兩個問題的解決方案要好得多。
看看它在操場上的例子是如何作業的
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489696.html
