0

I'm trying to do some aggregating with Mongo and I keep getting the error "Exception while invoking method 'pieChart' MongoError: exception $group does not support inclusion-style expressions" This is a Meteor project and I have installed the meteorhacks:aggregate package and have aggregations working in other areas of my app. This is server side code, btw. Thanks. Here is all the code:

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');
    }

Update: after some tinkering, this works. And I placed it within the for loop. Not positive, but it started working after I formatted the moment objects which turns them into strings.

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"}
          }
        }
      ];
prw416
  • 3
  • 4
  • Not sure if it's related or not but your `time` variable is a `moment` - did you mean to do `time.hour()`? You're also using that as the `_id` in the group expression... something just doesn't seem right. – chazsolo Mar 28 '16 at 20:29
  • You may be onto something but I feel like that would not cause the error about `inclusion style expressions` - that makes me think it may have something to do with the `$and` I'm trying to use. I'll replace the moment variables with strings and see what happens. – prw416 Mar 28 '16 at 20:42
  • @prw416 No it has to do with the `_id`. I highly doubt your stored value in `data.hour` is actually matching the `time` moment object anyway. If you just want to group for "everything" that was matched, simply use `null` for the `_id` i.e `{ "$group": { "_id": null, "spend": { "$sum": "data.spend" } }}`. Also be careful about those "embedded" values on `data`, since if those are actually within an "array" then you also need to handle differently, otherwise they are going to return as `0`. – Blakes Seven Mar 29 '16 at 03:42
  • If you're still really unsure about this, [edit](http://stackoverflow.com/posts/36270562/edit) your question with a sample document. That makes it easy to show you the correct thing to do. – Blakes Seven Mar 29 '16 at 03:43

0 Answers0