Possible bug in aggregation framework ? Mongo aggregation framework unable to sort by two fields on big collections?

207 views
Skip to first unread message

Matthieu Rigal

unread,
Apr 4, 2014, 4:56:36 AM4/4/14
to mongod...@googlegroups.com
Hi guys,

I am having a strange behaviour for a double-sorted output of an aggregation. I've posted the problem over here (http://stackoverflow.com/questions/22817921/mongo-aggregation-framework-unable-to-sort-by-two-fields) and the answer seems to comfort me in my assumption there might be something buggy.

Maybe it is a problem of ordering on a couple thousands of results without having an index on the result fields I want to sort by. Anyway to overcome this ?

For info, I am using Mongo 2.4.6 / 2.4.9 with PyMongo 2.6.3. I have currently 3Million entries, aggregated in a couple thousands results.

Thanks in advance,
Matthieu

---- 
Copied for info:

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 ONLY 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 ?

Here is a sample doc:

{
  "_id": { "$oid" : "533d0a3b830f783478a75aa1" },
  "_cls": "class1",
  "v_id": 6813,
  "total": {
    "amount": 680,
    "currency": "EUR",
    "exp": -2
  }
}

Bernie Hackett

unread,
Apr 4, 2014, 9:34:35 AM4/4/14
to mongod...@googlegroups.com
{"$sort": {"_id.currency": 1, "total": -1}}

Python dicts do not preserve key order. Use bson.son.SON (an ordered dict) for this:

from bson.son import SON
...

{"$sort": SON([("_id.currency", 1), ("total": -1)])}


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/ec9665b8-ffaf-42b4-908d-f4d5a7b0f047%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthieu Rigal

unread,
Apr 4, 2014, 10:11:13 AM4/4/14
to mongod...@googlegroups.com
Brilliant ! I completely forgot that in the MongoDB context !

Even better is to use Ordered Dict !

        from collections import OrderedDict
        sort_dict = OrderedDict()
        sort_dict['_id.currency'] = 1
        sort_dict['total'] = -1

IMHO this should be written and highlighted in the PyMongo documentation ! 

Thanks a lot Bernie !

Matthieu
Reply all
Reply to author
Forward
0 new messages