gpt4 book ai didi

MongoDB聚合三个集合

转载 作者:IT老高 更新时间:2023-10-28 13:27:56 27 4
gpt4 key购买 nike

这两天学习MongoDB,正在尝试聚合三个集合但无法实现

以下是数据库中维护的四个集合

大学

{
"_id" : "5834ecf7432d92675bde9d82",
"name": "NIFT"
}

大学

{
"_id" : "5834ecf7432d92675bde9d83",
"name": "NIFT Hyderabad",
"university_id":"5834ecf7432d92675bde9d82"
}

部门

{
"_id" : "5834ecf7432d92675bde9d84",
"department_name": "Fashion Technology",
"college_id" : "5834ecf7432d92675bde9d83"
},
{
"_id" : "5834ecf7432d92675bde9d85",
"department_name": "Merchandising",
"college_id" : "5834ecf7432d92675bde9d83"
}

部分

{
"_id" : "5834ecf7432d92675bde9d86",
"section_name": "A",
"students" : "56",
"department_id":"5834ecf7432d92675bde9d84"
},
{
"_id" : "5834ecf7432d92675bde9d87",
"section_name": "B",
"students" : "60",
"department_id":"5834ecf7432d92675bde9d84"
},
{
"_id" : "5834ecf7432d92675bde9d86",
"section_name": "A",
"students" : "55",
"department_id":"5834ecf7432d92675bde9d85"
},
{
"_id" : "5834ecf7432d92675bde9d87",
"section_name": "B",
"students" : "44",
"department_id":"5834ecf7432d92675bde9d85"
}

这里我试图实现以下格式的输出

预期输出

[{
"_id": "5834ecf7432d92675bde9d83",
"name": "NIFT Hyderabad",
"university_id": "5834ecf7432d92675bde9d82",
"departments": [{
"_id": "5834ecf7432d92675bde9d84",
"department_name": "CSE",
"college_id": "5834ecf7432d92675bde9d83",
"sections": [{
"_id": "5834ecf7432d92675bde9d86",
"section_name": "A",
"students": "56",
"department_id": "5834ecf7432d92675bde9d84"
}, {
"_id": "5834ecf7432d92675bde9d87",
"section_name": "B",
"students": "60",
"department_id": "5834ecf7432d92675bde9d84"
}]
},
{
"_id": "5834ecf7432d92675bde9d85",
"department_name": "Mechanical",
"college_id": "5834ecf7432d92675bde9d83",
"sections": [{
"_id": "5834ecf7432d92675bde9d86",
"section_name": "A",
"students": "55",
"department_id": "5834ecf7432d92675bde9d85"
},
{
"_id": "5834ecf7432d92675bde9d87",
"section_name": "B",
"students": "44",
"department_id": "5834ecf7432d92675bde9d85"
}
]
}
]
}]

但是,我在大学的单独数组中获取部门和部分,但无法以上述格式获取

查询

db.college.aggregate([
{"$match": { "university_id": "5834ecf7432d92675bde9d82" } },
{"$lookup": {
"localField": "_id",
"from": "departments",
"foreignField": "college_id",
"as": "departments"
}},
{"$unwind":"$departments"},
{$group : {_id : "$_id", departments : {$push : "$departments" }}},
{"$lookup": {
"localField": "departments._id",
"from": "sections",
"foreignField": "department_id",
"as": "sections"}
}
])

谁能帮我解决这个问题,对我很有帮助。

最佳答案

您可以尝试以下聚合查询。

下面的查询将 sections 推送到 department 中,然后将 $group 推送到部门以创建最终结构。

db.college.aggregate([
{
"$match": {
"university_id": "5834ecf7432d92675bde9d82"
}
},
{
"$lookup": {
"localField": "_id",
"from": "departments",
"foreignField": "college_id",
"as": "departments"
}
},
{
"$unwind": {
"path": "$departments",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"localField": "departments._id",
"from": "sections",
"foreignField": "department_id",
"as": "departments.sections"
}
},
{
"$group": {
"_id": "$_id",
"name": {
"$first": "$name"
},
"university_id": {
"$first": "$university_id"
},
"departments": {
"$push": "$departments"
}
}
}
])

关于MongoDB聚合三个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46403288/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com