我認爲Date Aggregation Operators可能是更好的選擇你的情況。鑑於您的架構如下
var dataSchema = new Schema({
// other fields are here...
updated: Date,
});
var Data = mongoose.model('Data', dataSchema);
只是將這些數據保存爲正常日期。
然後你就可以通過檢索aggregate operation in mongoose每小時塊,一個示例代碼一樣,
MyModel.aggregate([
{$match: {$and: [{updated: {$gte: start_date_hour}}, {updated: {$lte: end_date_hour}}]}},
{$group: {
_id: {
year: {$year: "$updated"},
month: {$month: "$updated"},
day: {$dayOfMonth: "$updated"}
// other fields should be here to meet your requirement
},
}},
{$sort: {"date.year":1, "date.month":1, "date.day":1}}
], callback);
對於aggregate
更多的參數,請參閱本doc。