相关推荐recommended
mongodb中的多表查询aggregate中排序不是按全表排序,而是当前页排序问题如何解决?
作者:mmseoamin日期:2024-04-27

在上篇文章中讲到了多表查询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函数中数组里的每个条件都是有顺序要求的&#