2017-09-25 149 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