【代码片段】云开发数据库查询,先group,再sort
发布于 4 年前 作者 juan61 4268 次浏览 来自 分享

今天在开发在线答题小程序的热度排名模块,需要统计每个部门的用户数,以及按照部门人数倒序 排列,马上就先到了group by xx sort by num desc

但是云开发数据库支持吗

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.group.html

1

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.sort.html#%E7%A4%BA%E4%BE%8B

2

经亲测支持,代码如下所示

0

1

    db.collection('profiles')
      .aggregate()
      .group({
          _id: '$dept',
          count: $.sum(1)
      })
      .sort({
        count: -1,
      })
      .end()
      .then(res => {
      // output: res.result === 3
    }).catch(err => {
      // handle error
    })

2

具体的界面截图如下所示

3

4

5

1 回复

收藏了慢慢看

回到顶部