收藏
回答

Aggregate 聚合分页查询操作时 .count()会改变输出结果?

首先,我想实现一个分页查询统计的功能,需要获取总条数和查询数据。

谁能告诉我为什么我 .count() 后,输出的 list 为啥变成了 [{total: 10}] ,我很不理解?????!!!

 const aggregateInstance = db.collection('deal').aggregate().sort({
      createTime: -1
    }).lookup({
      from'users',
      localField: '_userId',
      foreignField: '_id',
      as'userInfo',
    }).lookup({
      from'stores',
      localField: '_storeId',
      foreignField: '_id',
      as'storeInfo',
    }).lookup({
      from'users',
      localField: '_operatorId',
      foreignField: '_id',
      as'operatorInfo',
    }).match({
      'userInfo.nickName': db.RegExp({
        regexp: '.*' + keyword,
        options: 'i',
      })
    })
    const totalResult = await aggregateInstance.count('total').end()  
    console.log(totalResult);  // {list:[{total: 24}], errMsg: "collection.aggregate:ok"}
    const total = totalResult.list[0] && totalResult.list[0].total
    const {list} = await aggregateInstance.skip((pageIndex - 1) * pageSize).limit(pageSize).end()
     console.log(list); // [{total: 24}]

 

回答关注问题邀请回答
收藏

1 个回答

  • 李鸣晨
    李鸣晨
    2022-08-01

    强制自己记住吧

    2022-08-01
    有用
    回复 1
    • 誰
      2022-08-01
      所以说没有其他解决方法吗?统计总条数的话只能重写一遍上面的吗
      2022-08-01
      回复
登录 后发表内容
问题标签