2

さまざまなフィールドの数を取得するために mongodb 集計を使用しています。コレクションからいくつかの文書を以下に示しmobileます:-

{
  "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
  }
4

1 に答える 1

4

次のパイプラインのように、manufacturerおよびキーでドキュメントをグループ化し、ごとに評価を集計しながらratingさらにグループ化する集計操作の後だと思います。manufacturermanufacturer

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
}
于 2016-09-27T11:07:07.920 に答える