以聚合顺序使用 $$ROOT [英] Using $$ROOT in aggregation order

查看:49
本文介绍了以聚合顺序使用 $$ROOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm try get data from collection with some order:

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
}

我哪里做错了?

推荐答案

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

So per the comment you need a MongoDB version 2.6 do do this. But of course using 2.6 this works for me:

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

这篇关于以聚合顺序使用 $$ROOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆