MongoDB 聚合三个集合

8

我过去两天一直在学习MongoDB,试图聚合三个集合但无法实现。

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

university

{
   "_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"}
        }
        ])

有没有人能帮我解决这个问题,对我来说将非常有帮助。

1个回答

7
您可以尝试以下聚合查询。
当连接时,下面的查询将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"
      }
    }
  }
])

1
谢谢@Veeram,它有效。我还遇到了另一个问题,学院中有一个条目,但没有该学院的部门和部分条目,在这种情况下,无法在上面的查询中仅获取该学院的条目。只有当学院有部门条目时,我才能获取。 - Nagaraj ks
{ "$unwind": { "path": "$departments", "preserveNullAndEmptyArrays": true } }, 我已经更新了unwind以匹配空值,它可以正常工作。@Veeram,请更新相同的内容。感谢您的时间,这真的很有帮助,我学到了新的东西。 - Nagaraj ks
如果你想从大学开始而不是学院怎么办? - JenuRudan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接