Meteor js. How to sum records per month of the same collection

≡放荡痞女 提交于 2019-12-14 03:04:07

问题


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 invoices


回答1:


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

var start = new Date(year, month, day);
var end = new Date(year, month, day);

//Invoices with a 'date' field between the 'start' and 'end' dates
var 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



来源:https://stackoverflow.com/questions/45464098/meteor-js-how-to-sum-records-per-month-of-the-same-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!