1
我有一個數據集看起來如何在MongoDB中加入兩個聚合結果?
{"BrandId":"a","SessionId":100,"Method": "POST"}
{"BrandId":"a","SessionId":200,"Method": "PUT"}
{"BrandId":"a","SessionId":200,"Method": "GET"}
{"BrandId":"b","SessionId":300,"Method": "GET"}
我通過brandid寫聚集數不同的會話ID:
db.collection.aggregate([
{$group: {
"_id": {
brand: "$BrandId",
session: "$SessionId"
},
count: {$sum: 1}
}},
{$group: {
_id: "$_id.brand",
countSession:{$sum:1}
}}
])
查詢的預期結果是:
{ "_id" : "a", "countSession" : 2 }
{ "_id" : "b", "countSession" : 1 }
另一個查詢根據品牌計算方法是POST的地方:
db.collection.aggregate([
{$match: {Method:"POST"}},
{$group: {
_id: '$BrandId',
countPOST:{$sum:1}
}}
])
預期的結果:
{ "_id" : "a", "countPOST" : 1 }
{ "_id" : "b", "countSession" : 0 }
而現在,我想這兩個查詢結合起來,得到預期的結果如下:
{"BrandId:"a","countSession":2,"countPOST":1}
{"BrandId:"b","countSession":1,"countPOST":0}
我也不怎麼的這兩個結果結合起來兩個聚合,任何人都可以幫忙?