2017-04-11 152 views
1

我一直在嘗試對統計頁面的註冊集合數據進行分組和計數,以及進行動態註冊,但我無法讓它計算多個分組。 抽樣登記收集數據:使用嵌套聚合進行計數

{ 
     "_id" : ObjectId("58ec60078cc818505fb75ace"), 
     "event" : "Women's BB", 
     "day" : "Saturday", 
     "group" : "nonpro", 
     "division" : "Women's", 
     "level" : "BB" 
} 
{ 
     "_id" : ObjectId("58ec60078cc818505fb75acf"), 
     "event" : "Coed BB", 
     "day" : "Sunday", 
     "group" : "nonpro", 
     "division" : "Coed", 
     "level" : "BB" 
} 
{ 
     "_id" : ObjectId("58ec60098cc818505fb75ad0"), 
     "event" : "Men's BB", 
     "day" : "Saturday", 
     "group" : "nonpro", 
     "division" : "Men's", 
     "level" : "BB" 
} 
{ 
     "_id" : ObjectId("58ec60168cc818505fb75ad1"), 
     "event" : "Men's B", 
     "day" : "Saturday", 
     "group" : "nonpro", 
     "division" : "Men's", 
     "level" : "B" 
} 
{ 
     "_id" : ObjectId("58ec60178cc818505fb75ad2"), 
     "event" : "Women's Open", 
     "day" : "Saturday", 
     "group" : "pro", 
     "division" : "Women's", 
     "level" : "Pro" 
} 
{ 
     "_id" : ObjectId("58ec60188cc818505fb75ad3"), 
     "event" : "Men's Open", 
     "day" : "Saturday", 
     "group" : "pro", 
     "division" : "Men's", 
     "level" : "Pro" 
} 

我想重新組織,並做計數返回是這樣的:

[ {_id: { day: "Saturday", group: "nonpro" }, 
    count: 3, 
    divisions: [ 
    { division: "Men's", 
     count: 2, 
     levels: [ 
      { level: "BB", count: 1 }, 
      { level: "B", count: 1 }] 
    }, 
    { division: "Women's", 
     count: 1, 
     levels: [ 
      { level: "BB", count: 1 }] 
    } 
}, 
{_id: { day: "Saturday", group: "pro" }, 
    count: 2, 
    divisions: [ 
    { division: "Men's", 
     count: 1, 
     levels: [ 
      { level: "Pro", count: 1 } 
    }, 
    { division: "Women's", 
     count: 1, 
     levels: [ 
      { level: "Pro", count: 1 }] 
    } 
}, 
{_id: { day: "Sunday", group: "nonpro" }, 
    count: 1, 
    divisions: [ 
    { division: "Coed", 
     count: 1, 
     levels: [ 
      { level: "BB", count: 1 } 
    } 
}] 

我知道我應該使用聚合()函數,但我有很難讓它和伯爵一起工作。這裏是我的總的樣子至今:

Registration 
    .aggregate( 
    { $group: { 
     _id: { day: "$day", group: "$group" }, 
     events: { $addToSet: { division: "$division", level: "$level"} }, 
     total: { $sum: 1} 
    } 
    }) 

這將返回每天/組的組合總登記,但如果我嘗試添加總數:{$總和:1}的事件設置,我只是得到1 (這是有道理的)。有一種方法可以在一次數據庫調用中完成這項工作,還是需要爲需要考慮的每個分組級別單獨執行此操作?

回答

1

您基本上需要3個級別的$group流水線階段。第一個將通過全部四個密鑰對文件進行分組,即day,group,divisionlevel。彙總 組的計數,這將成爲level的計數。

前面的組將採取三個鍵即daygroupdivision和骨料計數將總結前面的組計數以及創建levels陣列。

最後一組將是daygroup鍵+ divisions列表中嵌入前一組的結果。

考慮運行下面的管道的預期成果:

Registration.aggregate([ 
    { 
     "$group": { 
      "_id": { 
       "day": "$day", 
       "group": "$group", 
       "division": "$division", 
       "level": "$level" 
      }, 
      "count": { "$sum": 1 } 
     } 
    }, 
    { 
     "$group": { 
      "_id": { 
       "day": "$_id.day", 
       "group": "$_id.group", 
       "division": "$_id.division"     
      }, 
      "count": { "$sum": "$count" }, 
      "levels": { 
       "$push": { 
        "level": "$_id.level", 
        "count": "$count" 
       } 
      } 
     } 
    }, 
    { 
     "$group": { 
      "_id": { 
       "day": "$_id.day", 
       "group": "$_id.group"    
      }, 
      "count": { "$sum": "$count" }, 
      "divisions": { 
       "$push": { 
        "division": "$_id.division", 
        "count": "$count", 
        "levels": "$levels" 
       } 
      } 
     } 
    } 
], (err, results) => { 
    if (err) throw err; 
    console.log(JSON.stringify(results, null, 4)); 
}) 

樣本輸出

/* 1 */ 
{ 
    "_id" : { 
     "day" : "Saturday", 
     "group" : "nonpro" 
    }, 
    "count" : 3, 
    "divisions" : [ 
     { 
      "division" : "Women's", 
      "count" : 1, 
      "levels" : [ 
       { 
        "level" : "BB", 
        "count" : 1 
       } 
      ] 
     }, 
     { 
      "division" : "Men's", 
      "count" : 2, 
      "levels" : [ 
       { 
        "level" : "BB", 
        "count" : 1 
       }, 
       { 
        "level" : "B", 
        "count" : 1 
       } 
      ] 
     } 
    ] 
} 

/* 2 */ 
{ 
    "_id" : { 
     "day" : "Saturday", 
     "group" : "pro" 
    }, 
    "count" : 2, 
    "divisions" : [ 
     { 
      "division" : "Women's", 
      "count" : 1, 
      "levels" : [ 
       { 
        "level" : "Pro", 
        "count" : 1 
       } 
      ] 
     }, 
     { 
      "division" : "Men's", 
      "count" : 1, 
      "levels" : [ 
       { 
        "level" : "Pro", 
        "count" : 1 
       } 
      ] 
     } 
    ] 
} 

/* 3 */ 
{ 
    "_id" : { 
     "day" : "Sunday", 
     "group" : "nonpro" 
    }, 
    "count" : 1, 
    "divisions" : [ 
     { 
      "division" : "Coed", 
      "count" : 1, 
      "levels" : [ 
       { 
        "level" : "BB", 
        "count" : 1 
       } 
      ] 
     } 
    ] 
} 
+1

我的英雄!這完全符合我的需要。 $ group和「_id」之間的引號很重要,我不太明白,但我會隨它一起去的! – user3561890