我想为 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/