0

Reading MongoDb documentation about aggregation operation, I've found this:

db.orders.aggregate(
    [
        { $match: { status: "A" } },
        { $group: { _id: "$cust_id", total: { $sum: "$amount" } } },
        { $sort: { total: -1 } }
    ],
    {
         explain: true
    }

)

But, suppose I have another field named phone, how can I aggregate about cust_id and phone?

I've tried as follow:

{ $group: { _id: {"$cust_id", "$phone"}, total: { $sum: "$amount" } } },

But doesn't work

EDIT

I want this behaviour:

SELECT SUM(amount)
FROM orders
GROUP BY cust_id, phone

But I have this error (I must attach screenshot because by MongoChef I can't textual error):

enter image description here

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Joe Taras
  • 15,166
  • 7
  • 42
  • 55

1 Answers1

2

There is nothing to explain. It's just wrong syntax. Objects should have field names, not only field values. E.g.:

{ $group: { _id: {id:"$cust_id", phone:"$phone"}, total: { $sum: "$amount" } } },
Alex Blex
  • 34,704
  • 7
  • 48
  • 75