14
這組如何將數據週報:MongoDB的組,每2周
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"}
}}
])
什麼格式有你的日期字段? – rubenfa
它是一個標準化的mongodb ISODate例如ISODate(「2013-12-25T17:00:00Z」)顯示時 –