总计$ bucket之后,我如何才能分组?

时间:2019-01-30 10:20:27

标签: mongodb mongoose

我的聚合看起来像这样

db.getCollection('vehicles').aggregate(
[
 {
  '$unwind': {
   'path': '$_pricelist'
  }
 }, {
  '$match': {
   '_pricelist.nod': {
    '$eq': 1
   }
  }
 }, {
  '$bucket': {
   'groupBy': '$_pricelist.price', 
   'boundaries': [
    0, 20, 35, 50, 80, 150, 800
   ], 
   'default': 'Other', 
   'output': {
    'count': {
     '$sum': 1
    }, 
    'vehicles': {
     '$push': {
      'model': '$model', 
      'manufacturer': '$manufacturer'
     }
    }
   }
  }
 }
])

哪个结果

    /*1*/
{
 "_id": 0.0,
 "count": 1.0,
 "vehicles": [{
  "model": 26,
  "manufacturer": "Fiat"
 }]
}
/*2*/
{
 "_id": 20.0,
 "count": 25.0,
 "vehicles": [{
  "model": 201,
  "manufacturer": "Citroen"
 },
 {
  "model": 144,
  "manufacturer": "Peugeot"
 },
 {
  "model": 154,
  "manufacturer": "lancia"
 },
 {
  "model": 154,
  "manufacturer": "lancia"
 },
 {
  "model": 172,
  "manufacturer": "Fiat"
 },
 {
  "model": 172,
  "manufacturer": "Fiat"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 26,
  "manufacturer": "Fiat"
 },
 {
  "model": 27,
  "manufacturer": "Škoda"
 },
 {
  "model": 3,
  "manufacturer": "Peugeot"
 },
 {
  "model": 74,
  "manufacturer": "Ford"
 },
 {
  "model": 74,
  "manufacturer": "Ford"
 },
 {
  "model": 74,
  "manufacturer": "Ford"
 },
 {
  "model": 74,
  "manufacturer": "Ford"
 },
 {
  "model": 73,
  "manufacturer": "Suzuki"
 },
 {
  "model": 53,
  "manufacturer": "Dacia"
 },
 {
  "model": 37,
  "manufacturer": "Seat"
 },
 {
  "model": 38,
  "manufacturer": "Fiat"
 },
 {
  "model": 53,
  "manufacturer": "Dacia"
 },
 {
  "model": 301,
  "manufacturer": "Volkswagen"
 }]
}
/*3*/
{
 "_id": 35.0,
 "count": 41.0,
 "vehicles": [{
  "model": 2,
  "manufacturer": "Peugeot"
 },
 {
  "model": 305,
  "manufacturer": "Renault"
 },
 {
  "model": 305,
  "manufacturer": "Renault"
 },
 {
  "model": 306,
  "manufacturer": "Peugeot"
 },
 {
  "model": 306,
  "manufacturer": "Peugeot"
 },
 {
  "model": 44,
  "manufacturer": "Opel"
 },
 {
  "model": 44,
  "manufacturer": "Opel"
 },
 {
  "model": 250,
  "manufacturer": "Opel"
 },
 {
  "model": 250,
  "manufacturer": "Opel"
 },
 {
  "model": 107,
  "manufacturer": "Renault"
 },
 {
  "model": 107,
  "manufacturer": "Renault"
 },
 {
  "model": 28,
  "manufacturer": "Škoda"
 },
 {
  "model": 28,
  "manufacturer": "Škoda"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 312,
  "manufacturer": "Lada"
 },
 {
  "model": 301,
  "manufacturer": "Volkswagen"
 },
 {
  "model": 165,
  "manufacturer": "Volkswagen"
 },
 {
  "model": 265,
  "manufacturer": "Fiat"
 },
 {
  "model": 118,
  "manufacturer": "Citroen"
 },
 {
  "model": 9,
  "manufacturer": "Renault"
 },
 {
  "model": 9,
  "manufacturer": "Renault"
 },
 {
  "model": 39,
  "manufacturer": "Volkswagen"
 },
 {
  "model": 131,
  "manufacturer": "Volkswagen"
 },
 {
  "model": 55,
  "manufacturer": "Opel"
 },
 {
  "model": 71,
  "manufacturer": "Škoda"
 },
 {
  "model": 83,
  "manufacturer": "Audi"
 },
 {
  "model": 205,
  "manufacturer": "Mazda"
 },
 {
  "model": 61,
  "manufacturer": "Renault"
 },
 {
  "model": 265,
  "manufacturer": "Fiat"
 },
 {
  "model": 12,
  "manufacturer": "Renault"
 },
 {
  "model": 71,
  "manufacturer": "Škoda"
 },
 {
  "model": 71,
  "manufacturer": "Škoda"
 },
 {
  "model": 240,
  "manufacturer": "Peugeot"
 },
 {
  "model": 74,
  "manufacturer": "Ford"
 },
 {
  "model": 201,
  "manufacturer": "Citroen"
 },
 {
  "model": 201,
  "manufacturer": "Citroen"
 },
 {
  "model": 120,
  "manufacturer": "Renault"
 },
 {
  "model": 120,
  "manufacturer": "Renault"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 50,
  "manufacturer": "Opel"
 },
 {
  "model": 307,
  "manufacturer": "Peugeot"
 }]
}
/*4*/
{
 "_id": 50.0,
 "count": 21.0,
 "vehicles": [{
  "model": 44,
  "manufacturer": "Opel"
 },
 {
  "model": 44,
  "manufacturer": "Opel"
 },
 {
  "model": 44,
  "manufacturer": "Opel"
 },
 {
  "model": 28,
  "manufacturer": "Škoda"
 },
 {
  "model": 205,
  "manufacturer": "Mazda"
 },
 {
  "model": 106,
  "manufacturer": "Ford"
 },
 {
  "model": 60,
  "manufacturer": "Renault"
 },
 {
  "model": 186,
  "manufacturer": "Renault"
 },
 {
  "model": 229,
  "manufacturer": "Toyota"
 },
 {
  "model": 242,
  "manufacturer": "BMW"
 },
 {
  "model": 215,
  "manufacturer": "Toyota"
 },
 {
  "model": 219,
  "manufacturer": "Volkswagen"
 },
 {
  "model": 193,
  "manufacturer": "Mazda"
 },
 {
  "model": 4,
  "manufacturer": "Peugeot"
 },
 {
  "model": 272,
  "manufacturer": "Mercedes"
 },
 {
  "model": 311,
  "manufacturer": "Dodge"
 },
 {
  "model": 30,
  "manufacturer": "Škoda"
 },
 {
  "model": 60,
  "manufacturer": "Renault"
 },
 {
  "model": 30,
  "manufacturer": "Škoda"
 },
 {
  "model": 307,
  "manufacturer": "Peugeot"
 },
 {
  "model": 4,
  "manufacturer": "Peugeot"
 }]
}
/*5*/
{
 "_id": 80.0,
 "count": 6.0,
 "vehicles": [{
  "model": 308,
  "manufacturer": "Fiat"
 },
 {
  "model": 308,
  "manufacturer": "Fiat"
 },
 {
  "model": 136,
  "manufacturer": "Volvo"
 },
 {
  "model": 208,
  "manufacturer": "Audi"
 },
 {
  "model": 157,
  "manufacturer": "Mercedes"
 },
 {
  "model": 87,
  "manufacturer": "Mercedes"
 }]
}
/*6*/
{
 "_id": 150.0,
 "count": 4.0,
 "vehicles": [{
  "model": 54,
  "manufacturer": "Mercedes"
 },
 {
  "model": 310,
  "manufacturer": "BMW"
 },
 {
  "model": 70,
  "manufacturer": "Audi"
 },
 {
  "model": 309,
  "manufacturer": "Range"
 }]
}

我在$ bucket之后尝试了$ group,然后尝试$ unwind,但这不是我所需要的。桶组必须保留。

所需结果类似于

{
 ...
 /*5*/
 "_id": 80.0,
 "count": 6.0,
 "models": [{
  "id": 308",
  "name": "Fiat",
  "count": 2
 },
 {
  "id": 136,
  "name": "Volvo",
  "count": 1
 },
 {
  "id": 208,
  "name": "Audi",
  "count": 1
 },
 {
  "id": 157,
  "name": "Mercedes",
  "count": 1
 },
 {
  "id": 87,
  "name": "Mercedes",
  "count": 1
 }]
 ...
}

基本上按模型在每个铲斗内对车辆进行分组并对其进行计数。 我是Mongo和Aggregation的新手(我最近发现了这一点,我非常喜欢)。

0 个答案:

没有答案
相关问题