I have been through about every example of aggregation and I can’t get it to work. I must be missing something small so here you go. Any help will be greatly appreciated.
First, what i want to accomplish is this. I have a table of transactions that I want to sum by Part/Movement and display the summed quantity by movement. But I can’t get anything to display on the screen.
This is what I get from Mongo.
db.movements.aggregate( [ { $group: { _id: “$Movement”, Quantity: { $sum: “$Quantity” } } } ] )
{ “_id” : “Complete”, “Quantity” : 50 }
{ “_id” : “Wip”, “Quantity” : 20 }
{ “_id” : “InStock”, “Quantity” : 20 }
{ “_id” : “OnOrder”, “Quantity” : 10 }
Here is my code.
Server: main.js
Meteor.publish("reportTotals", function() {
// Remember, ReactiveAggregate doesn't return anything
ReactiveAggregate(this, Movements, [{
$group: {
'_id': '$Movement',
'quantity': {
// In this case, we're running summation.
$sum: '$Quantity'
}
}
}, {
$project: {
quantity: '$Quantity'
} // Send the aggregation to the 'clientReport' collection available for client use
}], { clientCollection: "clientReport" });
});
client side js
Template.pstocks.helpers({
reportTotals: function() {
console.log("I'm working");
return clientReport.find();
},
});
html file:
{{#each reportTotals}}
Total Hours: {{quantity}}
{{/each}}