【问题标题】:exception: invalid operator $avg in mongoDB /例外:mongoDB / 中的无效运算符 $avg
【发布时间】:2016-04-25 15:35:37
【问题描述】:

您好,我使用的是 mongoDB 2.6.11

查询是:

db.companies.aggregate( [
{ $match: {  founded_year: { $eq: 2004 },  
    "funding_rounds.raised_amount": {$ne: null},  
     funding_rounds: { $size: 5 } } },
 { $project: { name:1,  
           _id:0, 
           avg: { $avg: "$funding_rounds.raised_amount" }} },
{ $sort: { avg: -1 } }
] );

但它给出了错误:

Error("Printing Stack Trace")@:0 ()@src/mongo/shell/utils.js:37 ([object Array])@src/mongo/shell/collection.js:866 @(shell):8

uncaught exception: aggregate failed: {   "errmsg" : "exception: invalid operator '$avg'",    "code" : 15999,     "ok" : 0 }

我在 robomongo 上运行此查询?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    在 mongoDB 文档中我们可以读到:

    The $avg is an accumulator operator available only in the $group stage.

    所以要使用$avg,您需要处于$group 阶段。

    【讨论】:

      猜你喜欢
      • 2013-06-14
      • 1970-01-01
      • 2017-05-21
      • 2020-12-19
      • 1970-01-01
      • 2012-08-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多