2017-04-09 212 views
1

我對MongoDB相當陌生,目前我正面臨一種情況。下面是從整個數據庫2條樣本記錄,我有:MongoDB彙總結果到嵌套數組

{ 
    "_id": 1, 
    "Record": 1, 
    "Link": [ "https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html" ], 
    "Location": [ "USA", "PAN", "USA", "USA", "PAN" ], 
    "Organization": [ "GN", "SOUTHCOM", "UCMJ", "PRC" ], 
    "Date": [ "2016" ], 
    "People": [ "P.Walter" ] 
} 
{ 
    "_id": 2, 
    "Record": 2, 
    "Link": [ "https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html" ], 
    "Location": [ "NIC", "GTM", "JAM", "GTM", "PAN" ], 
    "Organization": [ "CENTAM", "Calibre Mining Corporation", "STRATFOR", "Alder Resources" ], 
    "Date": [ "2013" ], 
    "People": [ "Daniel Ortega", "Hugo Chavez", "Paulo Gregoire" ] 
} 

基本上,我試圖讓一個像這樣的輸出:

{ 
    "Country": "US", 
    "Years": [ 
     { 
      "Year": "2016", 
      "Links": [ "https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html", 
      "https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html", 
      "https://wikileaks.org/gifiles/docs/90/9058_wax-12312008-csv-.html" ] 
     }, 
     { 
      "Year": "2013", 
      "Links": [ ""https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html", 
      "https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html", 
      "https://wikileaks.org/gifiles/docs/90/9058_wax-12312008-csv-.html" ] 
     } 
    ] 
"Link_Count": 6 
} 
    { 
    "Country": "UK", 
    "Years": [ 
     { 
      "Year": "2009", 
      "Links": [ "https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html", 
      "https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html", 
      "https://wikileaks.org/gifiles/docs/90/9058_wax-12312008-csv-.html" ] 
     }, 
     { 
      "Year": "2011", 
      "Links": [ ""https://wikileaks.org/gifiles/docs/11/111533_-latam-centam-brief-110822-.html", 
      "https://wikileaks.org/plusd/cables/1979PANAMA06344_e.html"] 
     } 
    ] 
"Link_Count": 5 
} 

我試着聚集,但我無法達到我想要的效果,就像我在輸出中給出的那樣。這是我的查詢:

db.test.aggregate([ 
{ 
"$unwind": "$Location" 
}, 
{ 
    "$group" : { 
     "_id": { 
      "Country": "$Location", 
      "Year": "$Date", 
      "Links": "$Link" 
     }, 
     Loc: { 
      $addToSet: "$Location" 
     } 
    } 
}, 
{ 
    "$unwind": "$Loc" 
}, 
{ 
    "$group": { 
     "_id": "$Loc", 
     "Years": { "$push": { 
      "Year": "$_id.Year", 
      "Links": "$_id.Links" 
      } 
     } 
    } 
} 
]).toArray() 

我在$ Location中使用了$ unwind和$ addToSet,因爲$ Location中有重複項。我接受任何建議或解決方案,請告訴我!提前致謝!

回答

0

您可以使用:

db.test.aggregate([{ 
    "$unwind": "$Location" 
}, { 
    "$unwind": "$Date" 
}, { 
    "$unwind": "$Link" 
}, { 
    "$group": { 
     "_id": { 
      "Country": "$Location", 
      "Year": "$Date" 
     }, 
     Links: { 
      $addToSet: "$Link" 
     } 
    } 
}, { 
    "$group": { 
     "_id": "$_id.Country", 
     Years: { 
      $push: { 
       "Year": "$_id.Year", 
       "Links": "$Links" 
      } 
     }, 
     Link_Count: { $sum: { $size: "$Links" } } 
    } 
}]) 

的想法是$unwind所有陣列能夠$push鏈接到一個新的數組,並計算與$size分組記錄最後$group階段。

+0

嗨,謝謝你的主意!我設法使用上面的查詢來獲得我想要的輸出。但是,我想進一步詢問我是否可以在Links上使用$ addToSet,因爲我只想得到唯一的鏈接,上面的查詢沒有給出,因爲它給了我重複。我真的很感激你的幫助,並且很抱歉給你帶來麻煩! – Levi0000

+0

查看我的更新答案,以獲得'Years.Links'數組中唯一的值,並讓'Link_Count'爲該國唯一的'Links'值的計數 –