In Mongodb, how do I sort by count first, then sort by time if there is a β€œtie”?

results = docDB.posts.find({"active":True }).sort("pop_score", pymongo.DESCENDING) 

This is my view right now. But the problem is that some things have the same "rating." In this case, if they contact, I want them to be sorted by "time" within those who tied.

How can I do it? This is possible to do in mysql ...

+6
sorting database mongodb pymongo
source share
1 answer

You can sort more than one attribute at a time. eg.

 sort({name : 1, age : -1}) 

will be sorted by name in ascending order and then by age

See here for reference: http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order

Edit:

In pymongo it will be

 .sort([['name', pymongo.ASCENDING], ['age', pymongo.DESCENDING]]) 

referenceL http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.sort

+13
source share

All Articles