我试图按日期对事件文档进行排序,以便"最接近"未来的事件是第一次,最远的事情是"客场比赛将持续到最后。
我的架构
// Event Schema
const EventSchema = mongoose.Schema({
dateStart: {
type: Date
},
dateEnd: {
type: Date
},
bsShiftType: {
type: String
},
adminID: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
});
我的获取方法+排序尝试
const d = new Date()
const today = new Date(d.getFullYear(), d.getMonth()-1, d.getDate()-1,23,59);
module.exports.getRequestEvents = function (parentID, callback) {
const query = {
parentID: parentID,
dateStart: {
$gte: today
}
};
Event.find(query).sort({ dateStart: -1 }).limit(1000).exec(callback);
}
结果 如你所见,它们没有正确排序。日子看起来很好,正在增加,但时间是相反的,他们正在下降。为什么呢?
0:{_id: "5a80e1ff4e93820834ee8db5", dateStart: "2018-02-13T23:00:00.000Z",…}
1:{_id: "5a809e0a0fed7c34043af6f1", dateStart: "2018-02-11T02:00:00.000Z",…}
2:{_id: "5a809e230fed7c34043af6f3", dateStart: "2018-02-11T01:00:00.000Z",…}
3:{_id: "5a809e1e0fed7c34043af6f2", dateStart: "2018-02-11T00:00:00.000Z",…}
4:{_id: "5a809fa082e6e113e41e483c", dateStart: "2018-02-11T00:00:00.000Z",…}
5:{_id: "5a809f8c82e6e113e41e483a", dateStart: "2018-02-11T00:00:00.000Z",…}
6:{_id: "5a809f9b82e6e113e41e483b", dateStart: "2018-02-11T00:00:00.000Z",…}
7:{_id: "5a809d560fed7c34043af6f0", dateStart: "2018-02-10T23:00:00.000Z",…}
8:{_id: "5a80e1cf4e93820834ee8db2", dateStart: "2018-01-12T23:00:00.000Z",…}
9:{_id: "5a80e1d74e93820834ee8db3", dateStart: "2018-01-11T23:00:00.000Z",…}
我想要实现的顺序是: 11th 00:00,11 01:00th ........,14th 02:00,14th 06:00