在mongodb中取消透视分组的列

时间:2018-09-05 10:55:14

标签: mongodb

我有一些分组的字段结果,并且想要将结果UNPIVOT放入mongodb 4.0中的行中。请帮忙。以下是查询:

    db.runCommand(
     {
    aggregate : 'testing',
    pipeline : [
       { "$group" : { "_id" :  null  , "currentDay" :
    { "$sum" : "$currentDay"/*Calculated Field */} ,
    "currentWeek" : { "$sum" : "$currentWeek"/*Calculated Field */} ,
    "prevWeek" : { "$sum" : "$prevWeek"/*Calculated Field */} ,
    "currentMonth" : { "$sum" : "$currentMonth"/*Calculated Field */} ,
    "currentYear" : { "$sum" : "$currentYear"/*Calculated Field */} ,
    "previousYear" : { "$sum" : "$prevYear"/*Calculated Field */}}}
   ]
   ,cursor : {batchSize : 10}
    }
 )


    Required Output : 
    [{
      d : 'Current Day',
      d1: 100 /*currentDay value */
      },
      {
      d : 'Current week',
      d1: 1000 /*currentWeek value */
      }         ,
     ----------------
        ]

致谢

克里斯

1 个答案:

答案 0 :(得分:0)

在上一个查询中,我通过Playground

达到了同样的效果
        {
     $project: {
        item: 1,
        dimensions: { $objectToArray: {'current Day':"$currentDay"
            ,'current Year' : '$currentYear','prev Year' : '$prevYear','current Month':'$currentMonth' }
     }
  }},
{$unwind : '$dimensions'} ,
  {$project : {d:'$dimensions.k',d1:'$dimensions.v',_id:0}} 

致谢

克里斯