云开发的云函数中如何使用 aggregate 对不同的状态进行合计?
发布于 5 年前 作者 gjia 9071 次浏览 来自 问答

假如list表的记录如下:

[
  {
    _id:0,
    progress:70,
  },
  {
    _id:1,
    progress:20,
  },
  {
    _id:3,
    progress:20,
  },
  {
    _id:4,
    progress:20,
  },
  {
    _id:5,
    progress:70,
  },
]

在云函数中如何使用 db.collection(‘list’).aggregate 一次性统计出 progress为70 和 progress为20 的总数,期望返回结果:

 {
  total_20:3,
  total_70:2
 }
回到顶部