mongodb - meteor react 骨料

标签 mongodb meteor

我想为 Transactions 集合创建 meteor react 聚合。

交易有日期,所以我想按月汇总数据。

代码是:

ReactiveAggregate(this, Transactions, [
    {
      $match: {
        'date': {
          $gte: new Date(startDate),
          $lt: new Date(endDate)
        }
      }
    },
    {
      '$group' :
      {
        '_id' : { month: { $month: "$date" }},
        'totalProfit': { $sum: "$totalProfit"},
        'totalSales': { $sum: "$totalSales" },
        'totalExpenses': { $sum: "$totalExpenses" },
        count: { $sum: 1 }
      }
    },
    {
      '$project':{
        date: '$date',
        totalProfit: '$totalProfit',
        totalSales: '$totalSales',
        totalExpenses: '$totalExpenses',
      }
    }
  ], { clientCollection: "report3MonthsTransactions" });

});

这样做会提示错误:

Error: Meteor does not currently support objects other than ObjectID as ids

谢谢!

最佳答案

您的$group子句是:

'$group' : {
  '_id' : { month: { $month: "$date" }},
  ...
}

这导致每个文档都有一个复合 _id : {_id: {month: <someMonth>}, ...} , 其中每个 _id是一个确实不是 ObjectID 的对象.

在您的情况下,拥有 {_id: <someMonth>, ...}就够了。

这可以通过如下分组来实现:

'$group' : {
  '_id' : { $month: "$date" },
  ...
}

顺便说一句,如果您需要 _id作为一个字符串(我认为你这样做),你可以使用 $substr转换它:

'$group' : {
  _id: {$substr: [{$month: '$date'}, 0, 2]},
  ...
}

关于mongodb - meteor react 骨料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37047541/

相关文章:

.net-4.0 - MongoDB 和复杂数组搜索

javascript - ReactJS:如何获取组件容器中的属性?

javascript - 如何对 Meteor 集合执行查找和排序(使用 Coffeescript)

MongoDB 文档嵌套文档过滤

javascript - 建筑时 meteor 坠落

javascript - 发布和订阅无法正常工作

javascript - 每个循环内的 jQuery meteor 不起作用

javascript - ReactJS 与传单 : tiles not correctly displayed until page refresh

javascript - Mongodb保存到嵌套对象中?

mongodb - 限制在组中推送返回的项目