如何按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结果
发布评论