【问题标题】:Using $$ROOT in aggregation order以聚合顺序使用 $$ROOT
【发布时间】:2014-05-25 12:38:12
【问题描述】:

我正在尝试按某种顺序从集合中获取数据:

db.data.aggregate([
    {$limit: 1000},
    {$group: {
        _id: "$service",
        count: {$sum: 1},
        data: {$push: '$$ROOT'}
    }}
]);

但是得到下一个错误:

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

uncaught exception: aggregate failed: {
    "errmsg" : "exception: FieldPath field names may not start with '$'.",
    "code" : 16410,
    "ok" : 0
}

我哪里做错了?

【问题讨论】:

  • 您实际使用的是 2.6 版本吗?它刚刚发布,因此文档现在反映了最新版本。

标签: mongodb aggregation-framework


【解决方案1】:

因此,根据评论,您需要 MongoDB 2.6 版来执行此操作。但是当然使用 2.6 这对我有用:

db.collection.aggregate([
    { "$limit": 1000 }, 
    { "$group": { 
        "_id": null, 
        "count": { "$sum": 1}, 
        "data": { "$push": "$$ROOT"  }
    }}
])

【讨论】:

  • 包名已更改!我只是更新 10gen 并获得 v2.4.10。谢谢!
  • @user2381111 感谢您说我忘记了反向移植。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 2022-01-14
  • 2020-07-30
  • 1970-01-01
  • 2017-01-24
相关资源
最近更新 更多