如何按多个对象元素分组

时间:2019-10-14 08:03:23

标签: mongodb mongoose

我有如下示例数据

[
    {
        brand:"iphone",
        category:"mobile"
    },
    {
        brand:"iphone",
        category:"laptop"
    },
    {
        brand:"lenova",
        category:"laptop"   
    }
]

,预期结果为

[
    {
        brand:"iphone",
        count:2
    },
    {
        brand:"lenova",
        count:1
    },
    {
        category:"laptop",
        count:2
    },
    {
        category:"mobile",
        count:1
    }
]

在这里,我要按具有多个字段的同一对象分组并在那里计数。谁能让我在猫鼬中做到这一点。

3 个答案:

答案 0 :(得分:0)

我不熟悉猫鼬。刚刚在Mongoshell中尝试过

db.getCollection('test').aggregate([
  { 
    $group:{
        _id:"$brand",
        brand:{$first:"$brand"},
        category:{$first:"$category"}

     }    
  },
  {$project:{_id:0}}
])

答案 1 :(得分:0)

仅通过使用两个查询才可能。

  
      
  1. 按品牌分组
  2.   
db.getCollection('pages').aggregate([   
    {
      $group: {_id: "$brand", category: { $push: "$category" }} 
    },
    {
      $project : {
        _id : 0, brand : "$_id", count : {$size : "$category"}
      }
    },
    { $unwind: { path: "$category", preserveNullAndEmptyArrays: true } }
])

结果:-

/* 1 */
{
    "brand" : "lenova",
    "count" : 1
}

/* 2 */
{
    "brand" : "iphone",
    "count" : 2
}
  
      
  1. 按类别分组
  2.   
db.getCollection('pages').aggregate([  
    {
        $group: {
            _id: "$category", brand: { $push: "$brand" },
        }
    },
    {
        $project : {
            _id : 0, category : "$_id", count : {$size : "$brand"}
        }
    },
    { $unwind: { path: "$brand", preserveNullAndEmptyArrays: true } },
])

结果:-

/* 1 */
{
    "category" : "laptop",
    "count" : 2
}

/* 2 */
{
    "category" : "mobile",
    "count" : 1
}

合并它们以获得所需的输出。

答案 2 :(得分:0)

我们可以使用 $facet 对数据进行并行聚合。

以下查询可以为我们提供预期的输出:

db.collection.aggregate([
  {
    $facet:{
      "brand_group":[
        {
          $group:{
            "_id":"$brand",
            "brand":{
              $first:"$brand"
            },
            "count":{
              $sum:1
            }
          }
        },
        {
          $project:{
            "_id":0
          }
        }
      ],
      "category_group":[
        {
          $group:{
            "_id":"$category",
            "category":{
              $first:"$category"
            },
            "count":{
              $sum:1
            }
          }
        },
        {
          $project:{
            "_id":0
          }
        }
      ]
    }
  },
  {
    $project:{ 
      "array":{
        $concatArrays:["$brand_group","$category_group"]
      }
    }
  },
  {
    $unwind:"$array"
  },
  {
    $replaceRoot:{ 
      "newRoot":"$array"
    }
  }
]).pretty()

数据集:

{
  "_id" : ObjectId("5da5c0d0795c8651a7f508c2"),
  "brand" : "iphone",
  "category" : "mobile"
}
{
  "_id" : ObjectId("5da5c0d0795c8651a7f508c3"),
  "brand" : "iphone",
  "category" : "laptop"
}
{
  "_id" : ObjectId("5da5c0d0795c8651a7f508c4"),
  "brand" : "lenova",
  "category" : "laptop"
}

输出:

{ "brand" : "lenova", "count" : 1 }
{ "brand" : "iphone", "count" : 2 }
{ "category" : "laptop", "count" : 2 }
{ "category" : "mobile", "count" : 1 }