流星 js。如何对同一集合的每月记录求和

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

我有发票合集,有tax字段,每个月都要调用发票,计算总税额。

我将 Momentjs 添加到 meteor 并使用了 blaze。

App invoices

您可以像这样在您的集合中搜索在特定月份创建的发票:

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 });

然后您可以找到税务字段的总和:

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

可以找到有关游标的 forEach 方法的更多信息 here