Help: aggregate($group $sum) --> how to call the result on client side?



background: dealing with sales data collection (SalesCollection) containing salesDate and salesAmount, autopublish and insecure is still NOT removed to simplify testing

objective: create daily report of sales amount per day

where I stand:

  1. installed meteorhacks:aggregate,
  2. created pipeline in server side $group: _id: salesDate $sum: “$salesAmount”
  3. SalesCollection.aggregate(pipeline);

then what? how do we call the results on client side? do we have to create new collection containing the result and find() said collection?

I must be very stupid, scoured the internet but all resources seem to only discuss about the pipeline stages and stop there (assuming people know what to do next --> unfortunately, I don’t).

Thanks in advance!


you can create the pipeline in a method on the server and call the method from the client with


Well, given function return results, so I suggest save them :smiley:

var aggregationResults =  SalesCollection.aggregate(pipeline);

Regarding getting results, there are few possibilities for temporary:

  1. method call which return them
  2. low level publish to client side collection
    Both would be non-reactive on server cause aggregation itself is, but it end up just in that how you prefer to process them on client. If you want it in minimongo (low level publish) or iterate over it in JS itself (method) where I suggest to look on underscore library which is in meteor core. For example to iterate over

Or you can insert it to separate report collection - mongo support it natively and you can specify target collection.
Which can be, but dont have to be configured on Meteor side.

and I suggest check tests for that package, you can see there how it is used


Thanks so much for the help.

Finally got it working. So the problem is two-fold:

  1. First we need to create or specify a method to publish the data (answered by two answers above), and then second

  2. apparently my aggregation itself is problematic to start with. Problem lies with HTML date input (salesDate) which records date in YYYY-MM-DD so my aggregation failed (cannot process BSON date). But after I parse the date (splicing it) and group by strings produced, the data magically appears.

    _id : {
    year: { $substr: [ “$salesDate”, 0, 4 ] },
    month: { $substr: [ “$salesDate”, 5, 2 ] },
    day: { $substr: [ “$salesDate”, 8, 2 ] } },

Then, I can call the result on console(log). Now I’m still working on displaying the results as method call.

Thanks again! Wonderful community!

EDIT: so I managed to return a result on server console.

as to choice #1, will it take performance toll from js calculations?
choice #2 I’m now crunching resources on “low-level publish” hopefully got enlightened soon.

as to reactivity, sales reports (historical) do not need to be reactive so my focus now is to produce the reports.

About the tests package… Honestly I’m having hard time decoding the js file trying to figure out what it does line by line.

is $out supported in Meteor 1.2.1? If yes, then this could be my easy way out. I tried inserting the result into a new collection and all it does is creating new document every time I refresh the page and furthermore i can’t process it into a nice list.


aggregation is not supported by 1.2.1
but still, with meteorhacks:aggregate it would work and $out would work too as these are just passed to mongo, nothing to do with Meteor.