异常:mongoDB中的运算符$ avg无效

时间:2016-04-25 15:35:38

标签: mongodb

您好我使用的是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上运行此查询?

1 个答案:

答案 0 :(得分:0)

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

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

因此,要使用$avg,您需要处于$group阶段。