I am doing some aggregation on a mongo 2.4.9 collection but I am not able to sort the result by two fields. Here is the query I am making using PyMongo:
result = mongo_coll.aggregate([{"$match": {"_cls": "class1"},
{"$group": {"_id": {"currency": "$total.currency",
"v_id": "$v_id"},
"total": {"$sum": "$total.amount"},
"count": {"$sum": 1}}},
{"$sort": {"_id.currency": 1, "total": -1}}])
And I have the result sorted by "total":-1
If I replace the last line with the following:
{"$sort": {"total": -1, "_id.currency": 1}}])
It is still sorted by "total":-1
And if I replace it with the following:
{"$sort": {"_id.currency": 1}}])
It gets sorted by currency.
But I can't get it sorted how I want, which means by currency first, and then by total... (The results else look good, as expected). Anybody has a clue ?
Best and thanks in advance !
UPDATE: Here is a sample doc:
{
"_id": { "$oid" : "533d0a3b830f783478a75aa1" },
"_cls": "class1",
"v_id": 6813,
"total": {
"amount": 680,
"currency": "EUR",
"exp": -2
}
}
currency
? can you post a sample document from your collection? – Anand Jayabalan Apr 2 '14 at 17:28