Skip to content Skip to sidebar Skip to footer

Meteor Js. How To Sum Records Per Month Of The Same Collection

I have the collection of invoices, with the tax field, I must call the invoices for each month and calculate the total tax. I have Momentjs add to meteor and uses blaze. App invoi

Solution 1:

You can search your collection for invoices created in a particular month like so:

var start = newDate(year, month, day);
var end = newDate(year, month, day);

//Invoices with a 'date' field between the 'start' and 'end' datesvar cursor = CollectionName.find({ date : { $gte : start, $lt: end });

You can then find the total of the tax fields:

var taxTotal = 0;
var results = cursor.forEach(function(doc) {
  //Adds the tax field of each document to the total
  taxTotal += doc.tax;
});

More information on the forEach method of a cursor can be found here

Post a Comment for "Meteor Js. How To Sum Records Per Month Of The Same Collection"