首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$accumulator (聚合)错误mongo数据库

$accumulator (聚合)错误mongo数据库
EN

Stack Overflow用户
提问于 2020-12-15 18:00:37
回答 1查看 202关注 0票数 0

需要帮助的mongo与双重说明,我复制了这一部分:

代码语言:javascript
复制
db.books.aggregate([
{
  $group :
  {
    _id : "$author",
    avgCopies:
    {
      $accumulator:
      {
        init: function() {                        // Set the initial state
          return { count: 0, sum: 0 }
        },

        accumulate: function(state, numCopies) {  // Define how to update the state
          return {
            count: state.count + 1,
            sum: state.sum + numCopies
          }
        },

        accumulateArgs: ["$copies"],              // Argument required by the accumulate function

        merge: function(state1, state2) {         // When the operator performs a merge,
          return {                                // add the fields from the two states
            count: state1.count + state2.count,
            sum: state1.sum + state2.sum
          }
        },

        finalize: function(state) {               // After collecting the results from all documents,
          return (state.sum / state.count)        // calculate the average
        },
        lang: "js"
      }
    }
  }
}
])

但是我得到了一个错误"errmsg“:”未知的组运算符'$accumulator'",https://docs.mongodb.com/manual/reference/operator/aggregation/accumulator/#grp._S_accumulator

EN

回答 1

Stack Overflow用户

发布于 2021-02-17 00:47:44

我也面临着同样的问题。检查您的MongoDB版本。$accumulator是4.4版中的新功能。

代码语言:javascript
复制
> mongo --version

附注:

我的MongoDB版本是4.2.5

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65303643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档