我认为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。