2017-09-25 110 views
4

学习的MongoDB在过去的两天,我试图聚集三个集合,但无法达到它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"} 
     } 
     ]) 

任何一个可以帮助我解决这个问题,这对我很有帮助。

回答

2

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

下面的查询在加入时将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它的工作原理。我还面临另一个问题,大学里有一个条目,没有该大学的部门和小组条目,在这种情况下,无法在上述查询中单独获得该大学的单独条目。我只能够得到,如果学院有部门条目 –

+0

{ “$开卷”:{“路径”:“$部门”,“preserveNullAndEmptyArrays”: 真} }, 我有更新的展开,以配合空的,它的工作原理,@Veeram请更新 感谢您的时间,这真的很有帮助,并学到了新的 –

+0

如果您想从大学开始而不是大学开始,该怎么办? – JenuRudan