我为结果创建了一个GET api,包括过滤器,分页和排序。但是,尽管数据是正确的,但排序却没有正确进行。
let skip = 0, limit = 10, findCond = {centerId: self.centersModel.getObjectId(userData.id)};
if(queryParams.result) findCond['result'] = {"$regex": queryParams.result, "$options": 'i'};
if(queryParams.fromDate) findCond['created_at'] = {$gte: moment(queryParams.fromDate + ' 00:00:00', 'DD-MM-YYYY HH:mm:ss').toDate()};
if(queryParams.toDate) {
if(findCond.hasOwnProperty('created_at')) findCond['created_at']['$lte'] = moment(queryParams.toDate + ' 23:59:59', 'DD-MM-YYYY HH:mm:ss').toDate();
else findCond['created_at'] = {$lte: moment(queryParams.toDate + ' 23:59:59', 'DD-MM-YYYY HH:mm:ss').toDate()};
}
if(queryParams.perPage) limit = parseInt(queryParams.perPage);
if(queryParams.page) skip = (parseInt(queryParams.page) - 1) * limit;
let aggregate = [
{ "$match": findCond },
{ "$sort": {"created_at": 1} },
{ "$skip": skip },
{ "$limit": limit }
];
console.log(aggregate)
self.resultsModel.aggregate(aggregate, function(err, results) {
console.log(err, results);
});
结果:
{
"status": "ok",
"results": [
{
"id": "5db83e69fcee977a20b24260",
"result": "Pass",
"examDate": "29/10/2019 06:58 PM"
},
{
"id": "5db6b4d33ffd7d3ccde175d1",
"result": "Pass",
"examDate": "28/10/2019 08:38 PM"
},
{
"id": "5db83e9bfcee977a20b24262",
"result": "Pass",
"examDate": "29/10/2019 06:58 PM"
},
{
"id": "5db83ecafcee977a20b24264",
"result": "Pass",
"examDate": "29/10/2019 06:59 PM"
},
{
"id": "5db83f40fcee977a20b24266",
"result": "Fail",
"examDate": "29/10/2019 07:01 PM"
},
{
"id": "5db84395bb402b0f3de43ff7",
"result": "Pass",
"examDate": "29/10/2019 07:20 PM"
},
{
"id": "5db843c0bb402b0f3de43ff9",
"result": "Pass",
"examDate": "29/10/2019 07:20 PM"
}
]
}
其中一个日期是2019年10月28日,在29/10之间。请告诉我一个原因或解决方案。
答案 0 :(得分:0)
我认为,为了减少混乱,您应该共享记录的聚合变量的值,而不是共享对其进行转换后创建的响应。