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

我相信您正在执行按manufacturerrating键对文档进行分组的聚合操作,然后manufacturer在聚合 per 的评级时进行进一步的分组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
}
于 2016-09-27T11:07:07.920 回答