Mongodb 流星反应聚集体
我想为事务集合创建meteor反应聚合 事务具有日期,因此我希望按月聚合数据 代码是:Mongodb 流星反应聚集体,mongodb,meteor,Mongodb,Meteor,我想为事务集合创建meteor反应聚合 事务具有日期,因此我希望按月聚合数据 代码是: ReactiveAggregate(this, Transactions, [ { $match: { 'date': { $gte: new Date(startDate), $lt: new Date(endDate) } } }, { '$group' : {
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" });
});
执行此操作时,将提示错误:
错误:Meteor当前不支持ObjectID以外的对象作为ID
谢谢 您的
$group
子句是:
“$group”:{
“_id”:{month:{$month:“$date”},
...
}
这导致每个文档都有一个复合的\u id
:{{u id:{month:},…}
,其中每个\u id
都是一个对象,实际上不是ObjectID
在您的情况下,拥有{u id:,…}
就足够了
这可以通过如下方式进行分组来实现:
“$group”:{
“_id”:{$month:$date”},
...
}
顺便说一句,如果您需要将\u id
作为字符串(我认为您需要这样做),您可以使用$substr
将其转换为:
“$group”:{
_id:{$substr:[{$month:'$date'},0,2]},
...
}
您是否尝试过简单地使用\u id:{$month:$date}
?