pymongo对分组结果进行排序
我需要使用 pymongo 按 date_published 存储在 mongodb 上的一些文档进行分组和排序。
组部分进展顺利:)但是当我将 .sort() 添加到查询中时,无论我尝试什么,它都会失败:(
这是我的查询:
db.activities.group(keyf_code,cond,{},reduce_code)
我想按名为“已发布”(时间戳)的字段进行排序 尝试过做
db.activities.group(keyf_code,cond,{},reduce_code).sort({"published": -1})
更多的变化,但没有任何成功的
想法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前您无法在 MongoDB 中对组进行排序。您可以使用 MapReduce 代替,它支持排序选项。还有一个增强请求,要求支持排序此处的群组。
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.
虽然 MongoDB 没有做你想做的事,但你总是可以使用 Python 来进行排序:
Although MongoDB doesn't do what you want, you can always use Python to do the sorting: