收藏
回答

请教一下,云数据库可以一次返回sum和count吗?

    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 个回答

  • 老张
    老张
    2020-03-18

    count = $.sum(1)

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

    2020-03-18
    有用 1
    回复 1
    • 名月
      名月
      2020-03-18
      和您料想的一样,非常感谢
      2020-03-18
      回复
登录 后发表内容
问题标签