在上篇文章中讲到了多表查询https://blog.csdn.net/guige8888811/article/details/133880362 其中还少一个知识点则是排序。而起初写了排序条件之后发现排序并不是按全表排的。代码如下:
await mongodb.getConnection('attendanceRecord').aggregate([ { $lookup: { from: 'userInfo', localField: 'userId', foreignField: 'userId', as: 'userInfo', }, }, { $unwind: '$userInfo' }, { $match: where, }, { $skip: Number(start), }, { $limit: Number(limit), },{ $sort: { 'createTime': -1 } } ]) .exec();
而找了很久才发现原因,其实aggregate函数中数组里的每个条件都是有顺序要求的
上一篇:缓存相关知识总结