【发布时间】:2014-01-17 13:38:06
【问题描述】:
这是每周对数据进行分组的方法:
transactions.aggregate({
[
{$match: {status: "committed"}},
{$group: {
_id: {
$year: "$date",
$week: "$date"
},
count: {$sum: 1},
start_date: {$min: "$date"},
end_date: {$max: "$date"}
}}
]
});
问题是,如何每 2 周分组一次并获得计数?
谢谢。
根据接受的答案的正确方法
感谢 Mzzl,这适用于每 2 周进行一次分组。以下是完整版。
db.transactions.aggregate([
{$match: {status: "committed"}},
{$project: {
twoweekperiod: {
$subtract: [
{$week: "$date"}, {$mod: [{$week: "$date"}, 2] }
]
},
date:1,
status:1
}},
{$group: {
_id: {
year: {$year: "$date"},
twoweek: "$twoweekperiod"
},
count: {$sum: 1},
start_date: {$min: "$date"},
end_date: {$max: "$date"}
}}
])
【问题讨论】:
-
你的日期字段是什么格式的?
-
这是一个标准化的 mongodb ISODate 例如ISODate("2013-12-25T17:00:00Z") 显示时
标签: mongodb mongoid aggregation-framework