请教一下,云数据库可以一次返回sum和count吗?
发布于 6 年前 作者 nachen 11065 次浏览 来自 问答
    const _ = db.command;
    const $ = _.aggregate;
    const res = await commentCollection
      .aggregate()
      .unwind('$commentArray')   
      .match({ 
        diner_id: event.x_id,})
      // .count('commentArray')
      .group({
        _id: null,
        totalCommentScore: $.sum('$commentArray.commentScore'),
        avgCommentScore: $.avg('$commentArray.commentScore'),
      })
      .end()

以上是我查询一个集合的方法,单独执行group获取sum和avg,或者count(被注释的部分),都是可以正确执行的。
但是不知道如何同时获取commentArray数组中对commentScore(number类型)的sum、count,请大家指导一下,谢谢。
1 回复

count = $.sum(1)

你试试吧,mongo里是这样的,不知道云开发是否一样。

回到顶部