尝试运行Mongo聚合时出错

时间:2016-03-28 20:25:18

标签: mongodb meteor

我正在尝试与Mongo进行一些聚合,并且我一直收到错误“调用方法时出现异常'pieChart'MongoError:异常$ group不支持包含样式表达式” 这是一个Meteor项目,我安装了meteorhacks:聚合包,并在我的应用程序的其他区域有聚合。这是服务器端代码,顺便说一句。 谢谢。以下是所有代码:

let twentyFourHourArray = [];
    let time = moment('12:00 am');
    let pipeline = [
      {
        $match: { 
          $and: [
            {'data.hour': time},
            {'data.campaign_id': { $in: campaignIds } }
          ]
        }
      },
      {$group: {
        _id: time,
        spend: {$sum: "$data.spend"},
        clicks: {$sum: "$data.clicks"},
        impressions: {$sum: "$data.impressions"},
        likes: {$sum: "$data.like"}
        }
      }
    ];
    for (var i = 0; i <= 24; i++) {
      let result = HourlyBreakdowns.aggregate(pipeline);
      result[0]['cpc'] = result[0].spend / result[0].clicks;
      result[0]['cpm'] = result[0].spend / (result[0].impressions / 1000);
      result[0]['cpl'] = result[0].spend / result[0].likes;
      twentyFourHourArray.push(result);
      time = time.add(1, 'hour');
    }

更新:经过一些修补,这是有效的。我把它放在for循环中。不是肯定的,但是在我格式化了将它们变成字符串的瞬间对象之后它就开始工作了。

var pipeline = [
        {$match:
          {$and: [
              {'data.hour': time.format('hh:mm a')},
              {'data.campaign_id': { $in: campaignIds } }
            ]
          }
        },
        {$group: {
          _id: time.format('hh:mm a'),
          spend: {$sum: "$data.spend"},
          clicks: {$sum: "$data.clicks"},
          impressions: {$sum: "$data.impressions"},
          likes: {$sum: "$data.like"}
          }
        }
      ];

0 个答案:

没有答案