按ObjectId日期汇总MongoDB结果

编程入门 行业动态 更新时间:2024-10-24 16:26:13
本文介绍了按ObjectId日期汇总MongoDB结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何按ObjectId日期汇总MongoDB结果.示例:

How can I aggregate my MongoDB results by ObjectId date. Example:

默认光标结果:

cursor = [ {'_id': ObjectId('5220b974a61ad0000746c0d0'),'content': 'Foo'}, {'_id': ObjectId('521f541d4ce02a000752763a'),'content': 'Bar'}, {'_id': ObjectId('521ef350d24a9b00077090a5'),'content': 'Baz'}, ]

预计结果:

projected_cursor = [ {'2013-09-08': {'_id': ObjectId('5220b974a61ad0000746c0d0'),'content': 'Foo'}, {'_id': ObjectId('521f541d4ce02a000752763a'),'content': 'Bar'} }, {'2013-09-07': {'_id': ObjectId('521ef350d24a9b00077090a5'),'content': 'Baz'} } ]

这是我目前在PyMongo中用于实现这些结果的方法,但是它很凌乱,我想看看如何使用MongoDB的聚合框架(甚至是MapReduce)来做到这一点:

This is what I'm currently using in PyMongo to achieve these results, but it's messy and I'd like to see how I can do it using MongoDB's aggregation framework (or even MapReduce):

cursor = db.find({}, limit=10).sort("_id", pymongo.DESCENDING) messages = [x for x in cursor] this_date = lambda x: x['_id'].generation_time.date() dates = set([this_date(message) for message in messages]) dates_dict = {date: [m for m in messages if this_date(m) == date] for date in dates}

是的,我知道最简单的方法是向每个记录添加一个新的日期字段,然后以此为依据进行汇总,但这不是我现在想做的.

And yes, I know that the easiest way would be to simply add a new date field to each record then aggregate by that, but that's not what I want to do right now.

谢谢!

推荐答案

所以这不能直接回答我的问题,但是我确实找到了一种更好的方法,可以使用Python的setdefault替换上面所有的lambda废话:

So this doesn't answer my question directly, but I did find a better way to replace all that lambda nonsense above using Python's setdefault:

d = {} for message in messages: key = message['_id'].generation_time.date() d.setdefault(key,[]).append(message)

感谢@raymondh暗示了PyCon谈话:

Thanks to @raymondh for the hint in is PyCon talk:

将代码转换为漂亮的惯用Python

更多推荐

按ObjectId日期汇总MongoDB结果

本文发布于:2023-11-22 18:26:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   ObjectId   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!