开发者

pymongo sort grouped results

I need to group and sort by date_published some documents stored on mongodb开发者_运维问答 using pymongo.

the group part went just fine :) but when I'm addding .sort() to the query it keeps failing no matter what I tried :(

here is my query:

db.activities.group(keyf_code,cond,{},reduce_code)  

I want to sort by a field called "published" (timestamp) tried to do

db.activities.group(keyf_code,cond,{},reduce_code).sort({"published": -1})  

and many more variations without any success

ideas anyone?


You can't currently do sort with group in MongoDB. You can use MapReduce instead which does support a sort option. There is also an enhancement request to support group with sort here.


Although MongoDB doesn't do what you want, you can always use Python to do the sorting:

result = db.activities.group(keyf_code,cond,{},reduce_code)
result = sorted(result, key=itemgetter("published"), reverse=True)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜