微信小程序开发的聚合函数排序.aggregate.sort

普通查询用.orderBy('add_time','desc'),聚合查询用.sort({ins_time:-1})
'use strict';
 const db = uniCloud.database() //对数据库的对象获取;
 
exports.main = async (event, context) => {
    let start = new Date().getTime();
    const collection = db.collection('usemall-visual') 
    const collection1 = db.collection('usemall-student') 

    let currentPage = event.currentPage;
    let pageSize = event.pageSize;

     // 查找数据 寻找key为 更新出现的       
    let res = await collection.aggregate()
    .skip(currentPage) // 跳过前currentPage条
    .limit(pageSize) // 获取pageSize条
    .sort({
        ins_time:-1   //1:升序,2:降序
    })
    .lookup({
        from: "usemall-student",
        localField: "uid",
        foreignField: "_id",
        as: "stuinfo"
    })
    .project({ //关联别名
        'stuinfo._id': true,
        'stuinfo.name': true,
        'stuinfo.age': true,
        'stuinfo.scgc': true,
        'left_ver': true,
        'right_ver': true,
        'all_ver': true,
        'ins_time': true,
        'uid': true,
        quantity: true
      })
    .end();
    
    let count = await collection.count();
    
    let response = {
        code: 1,
        order: []
    };
    
    response.data = res.data;
    response.count = count.total;
    response.name =  event.name;
    return response;

};

 

posted @ 2023-12-01 10:18  wangjinggang  阅读(161)  评论(0编辑  收藏  举报