您好我使用的是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上运行此查询?
答案 0 :(得分:0)
在mongoDB文档中我们可以读到:
The $avg is an accumulator operator available only in the $group stage.
因此,要使用$avg
,您需要处于$group
阶段。