Getting results from aggregation

Hi,

I am trying to build something like a timeline app. I have a Entries collection that I repeat over on my timeline. However, I want all my entries categorised by date like so:

12.02.2016

  • entry
  • entry
    30.02.2016
  • entry
  • entry
  • entry

To do this, I am using aggregation to group the entries with the same date.

db.entries.aggregate( [    { $group: { _id: "$date", entries: { $push: "$$ROOT" },  number: { $sum: 1 } } },    { $match: { } } ]);

It works in the mongo console, but I am stuck because I do not know how to get those results in a publication and rerun the aggregation when there are changes to the Entries collection.

I’m using the meteorhacks:aggregate package for this.

Can anyone help me ?

Hey @johnsnow, Head over to the The Meteor Chef for (yet another) great article, this one discusses the aggregation package your using.

1 Like

Well… db.entries.aggregate works but when I do Entries.aggregate - in order to get the aggregation into the frontend, I get [Object, Object] has no method aggregate

Entries is essentially a minimongo wrapper around a collection and does not have an aggregate method. What this means is that although you can perform aggregation operations on the server (basically by using the underlying collection) the results need to be explicitly returned to the client (call/method) or using a “roll-your-own” publication.

I was about to use meteorhacks:aggregate as described in the article. However $sum still exhibits the floating point imprecision flaw (0.1 + 0.2 === 0.3 is false), so should not be used for monetary calculations as done in the example.

1 Like