results = docDB.posts.find({"active":True }).sort("pop_score", pymongo.DESCENDING)
This is my sort right now. But the problem is, some things have the same "score". In that case, if they tie, I want them to sort by "time" within the ones who tied.
How do I do that? It's possible to do that in Mysql...
We can sort the data using the aggregation method, aggregation method is very useful to sort the date field in ascending or descending order. We can also use order by operator to sort the date type field documents in MongoDB. While using any operator we need to specify the value of ascending or descending order.
Ascending/Descending SortSpecify in the sort parameter the field or fields to sort by and a value of 1 or -1 to specify an ascending or descending sort respectively. When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest: MinKey (internal type)
You can sort by more than one attribute at a time. e.g.
sort({name : 1, age : -1})
will sort by name ascending then by age descending
See here for reference: http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order
Edit:
In pymongo, that would be
.sort([['name', pymongo.ASCENDING], ['age', pymongo.DESCENDING]])
referenceL http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.sort
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With