2

我使用mongodb聚合獲取不同字段的計數。下面是從mobile收集一些文件: -Mongodb在單個聚合中使用多個組運算符

{ 
    "title": "Moto G", 
    "manufacturer": "Motorola", 
    "releasing": ISODate("2011-03-00T10:26:48.424Z"), 
    "rating": "high" 
} 
{ 
    "title": "Asus Zenfone 2", 
    "manufacturer": "Asus", 
    "releasing": ISODate("2014-10-00T10:26:48.424Z"), 
    "rating": "high" 
} 
{ 
    "title": "Moto Z", 
    "manufacturer": "Motorola", 
    "releasing": ISODate("2016-10-12T10:26:48.424Z"), 
    "rating": "none" 
} 
{ 
    "title": "Asus Zenfone 3", 
    "manufacturer": "Asus", 
    "releasing": ISODate("2016-08-00T10:26:48.424Z"), 
    "rating": "medium" 
} 

我能找到manufacturerrating計數,但這個失敗:

db.mobile.aggregate([ 
    { 
     $group: { _id: "$manufacturer", count: { $sum: 1 } } 
    }, { 
     $group: { _id: "$rating", count: { $sum: 1 } } 
    } 
]) 

輸出: -

{ 
    "_id" : null, 
    "count" : 2.0 
} 

預期輸出是這樣的: -

{ 
     "_id":"Motorola", 
     "count" : 2.0 
    } 
    { 
     "_id":"Asus", 
     "count" : 2.0 
    } 
    { 
     "_id":"high", 
     "count" : 2.0 
    } 
    { 
     "_id":"none", 
     "count" : 1.0 
    } 
    { 
     "_id":"medium", 
     "count" : 1.0 
    } 

回答

3

我相信你是一個集合的操作,組由manufacturerrating鍵的文件,然後做進一步的組上manufacturer而聚集的收視率每manufacturer,像下面的管道進行:

db.mobile.aggregate([ 
    { 
     "$group": { 
      "_id": { 
       "manufacturer": "$manufacturer", 
       "rating": "$rating" 
      }, 
      "count": { "$sum": 1 } 
     } 
    }, 
    { 
     "$group": { 
      "_id": "$_id.manufacturer", 
      "total": { "$sum": 1 }, 
      "counts": { 
       "$push": { 
        "rating": "$_id.rating", 
        "count": "$count" 
       } 
      } 
     } 
    } 
]) 

樣本輸出

/* 1 */ 
{ 
    "_id" : "Motorola", 
    "total" : 2, 
    "counts" : [ 
     { 
      "rating" : "high", 
      "count" : 1 
     }, 
     { 
      "rating" : "none", 
      "count" : 1 
     } 
    ] 
} 

/* 2 */ 
{ 
    "_id" : "Asus", 
    "total" : 2, 
    "counts" : [ 
     { 
      "rating" : "high", 
      "count" : 1 
     }, 
     { 
      "rating" : "medium", 
      "count" : 1 
     } 
    ] 
} 

,或者如果你是一個後更 「平」或 「去歸一化」 的結果,運行此聚合操作:

db.mobile.aggregate([ 
    { 
     "$group": { 
      "_id": "$manufacturer", 
      "total": { "$sum": 1 },   
      "high_ratings": { 
       "$sum": { 
        "$cond": [ { "$eq": [ "$rating", "high" ] }, 1, 0 ] 
       } 
      }, 
      "medium_ratings": { 
       "$sum": { 
        "$cond": [ { "$eq": [ "$rating", "medium" ] }, 1, 0 ] 
       } 
      }, 
      "low_ratings": { 
       "$sum": { 
        "$cond": [ { "$eq": [ "$rating", "low" ] }, 1, 0 ] 
       } 
      },    
      "none_ratings": { 
       "$sum": { 
        "$cond": [ { "$eq": [ "$rating", "none" ] }, 1, 0 ] 
       } 
      }   
     } 
    } 
]) 

樣本輸出

/* 1 */ 
{ 
    "_id" : "Motorola", 
    "total" : 2, 
    "high_ratings" : 1, 
    "medium_ratings" : 0, 
    "low_ratings" : 0, 
    "none_ratings" : 1 
} 

/* 2 */ 
{ 
    "_id" : "Asus", 
    "total" : 2, 
    "high_ratings" : 1, 
    "medium_ratings" : 1, 
    "low_ratings" : 0, 
    "none_ratings" : 0 
} 
+1

謝謝...你纔是天才在MongoDB中@chridam – vineet