[英]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. 我有Momentjs添加到流星并使用火焰。
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 有关游标的
forEach
方法的更多信息,请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.