我尝试使用Aggregate.sort对聚会结果进行排序,先看数据格式,如下图:
(month是标准时间,phoneNumber是字符串)
我的代码是这样的:
exports.main = async (event, context) => {
const { date, department } = event,
match = { class: '薪酬' };
if (date) {
const start = new Date(date.start.time),
end = new Date(date.end.time);
match.month = _.and(_.gt(start), _.lt(end))
}
if (department.department1 != '全部') match.department1 = department.department1;
if (department.department2 != '全部') match.department2 = department.department2;
// 查询并返回
const get = await db.collection('salary').aggregate()
.match(match)
.group({
_id: {
month: '$month',
department1: '$department1',
department2: '$department2',
name: '$name',
phoneNumber: '$phoneNumber',
paidInAmount: '$paidInAmount'
}
})
.sort({
phoneNumber: -1,
})
.limit(1000)
.end();
return get
};
结果,无论是对时间格式的字段(month),还是字符串格式的字段(phoneNumber),进行排序都不生效,输出如下:
即不能实现按时间先后顺序排序,或按字符串排序。
请教各位大佬,我的代码哪里写错了?感谢