How I can sum all the values of a property in a Meteor collection?

meteor, mongodb, sum

Solution

if you don't want to use Underscore:

var total = 0;

MyCollection.find({name:"test3"}).map(function(doc) {
  total += doc.price;
});

Problem

For example : ``` Object {_id: "9g9ySxozSWn1", name: "test1", price: 1} Object {_id: "9g9ySxozSWn2", name: "test2", price: 2} Object {_id: "9g9ySxozSWn3", name: "test3", price: 3} Object {_id: "9g9ySxozSWn4", name: "test3", price: 6} Object {_id: "9g9ySxozSWn5", name: "test3", price: 3} ``` What is the best way to sum up all the price where for example the name is test3?

Original source