Mongodb汇总了三个集合

Nag*_* ks 6 mongodb nosql mongodb-query aggregation-framework

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

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

大学

{
   "_id" : "5834ecf7432d92675bde9d82",
   "name": "NIFT"
}
Run Code Online (Sandbox Code Playgroud)

学院

{
   "_id" : "5834ecf7432d92675bde9d83",
   "name": "NIFT Hyderabad",
   "university_id":"5834ecf7432d92675bde9d82"
}
Run Code Online (Sandbox Code Playgroud)

部门

{
   "_id" : "5834ecf7432d92675bde9d84",
   "department_name": "Fashion Technology",
   "college_id" : "5834ecf7432d92675bde9d83" 
},
{
   "_id" : "5834ecf7432d92675bde9d85",
   "department_name": "Merchandising",
   "college_id" : "5834ecf7432d92675bde9d83"
}
Run Code Online (Sandbox Code Playgroud)

{
   "_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"
}
Run Code Online (Sandbox Code Playgroud)

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

预期产出

[{
   "_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"
            }
        ]
    }
  ]
}]
Run Code Online (Sandbox Code Playgroud)

但是,我在大学的单独阵列中获得了部门和部分,但是无法以上述格式获得

询问

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"}
        }
        ])
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题,对我来说非常有帮助.

use*_*814 5

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

下面的查询将它们加入时推sectionsdepartment,并$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"
      }
    }
  }
])
Run Code Online (Sandbox Code Playgroud)