云开发的云函数中如何使用 aggregate 对不同的状态进行合计?

发布于 7 年前作者 gjia9249 次浏览最后编辑 7 年前来自 ask

假如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
 }
2 回复