2
我想对文档集合执行一些聚合。该文件是这样的:MongoDB - 如何在聚合管道中使用多个组?
{name: "Sara", brandId: 1, count: 2 day:1/6/2014}//day value is ISODate
{name: "Sally", brandId: 1, count: 5 day:1/7/2014}
{name: "Mike", brandId: 1, count: 2, day: 1/8/2014}
{name: "Bob", brandId: 1, count: 4 day: 1/8/2014}
{name: "Joe", brandId: 1, count: 5 day:1/8/2014}
我所试图做的是让所有的“计数”值的总金额。然后,我想按日期范围对文档进行分组,并获得第二个分组的总和。因此,我愿做这样的事情:
db.people.aggregate(
{ $match : { BrandId : 4 } },
{ $group : {
_id : "$brandId",
TotalCount : { $sum : "$count" } //get the sum of all document 'count'
}},
{$match: {
'Day': { $gte: new Date(2014, 0, 6),
$lte: new Date(2014, 0, 8)}
}},
{ $group : {
_id : "$BrandId",
countInTimeRange : { $sum : "$count" } //get the sum of 'count' within time range
}}
)
所以我想获得的计数为所有匹配的文件,然后按日期范围过滤他们,并得到过滤的日期范围匹配的文档的总金额。 这可能吗?
谢谢!