I've been searching for answer to this question but sadly I don't see that will answer my question. I have an expense collection that looks like this
{ subcategory: 'Category-1', amount: 320 },
{ subcategory: 'Category-2', amount: 148 },
{ subcategory: 'Category-1', amount: 500 },
{ subcategory: 'Category-3', amount: 672 },
{ subcategory: 'Category-3', amount: 196 },
{ subcategory: 'Category-1', amount: 298 },
... etc
and I wanted to show in my summary report through my table like this
**Category Name** **Amount**
1. Category-1 1118
2. Category-2 148
3. Category-3 868
which I am then going to render these data on my table template
{{#each expensesentries}}
<tr>
<td>{{category}}</td>
<td>{{formatAmount amount}}</td>
</tr>
{{/each}} `
Thanks.
There may be a more elegant way to do this, but give this a try:
The first part aggregates the amounts by category into a single object. So
subs
will look like:The second part builds an array (
results
) with the necessary key-value pairs for your template. When done, it should look like:You should take care, that your approved solution requires that all data is stored on the client. What does it mean?
If you have a collection with about 1000 or even more entries than you have to publish all these entries and store these entries on the client before you can do a group. That requires a lot of space and costs lot of performance. So if your collection must not be reactive than you should look for a server side group via aggregation. Check this stackoverflow-post:
Aggregation via meteor
EDIT: Basically the linked article shows everything you need. I hope you are already common with publish and subscribe?
Check mongodb aggregation pipeline
If you use iron router than you should subscribe in there.
In your Template:
This query could also be definded in your iron-router data callback. Dont expect this to work out of the box. Read manuals of iron-router, mongodb aggregation and try to understand whats going on. Happy coding :)