Question

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

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

But get next error:

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
}

Where did I go wrong?

Was it helpful?

Solution

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"  }
    }}
])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top