Aggregate MongoDB results by ObjectId date

前端 未结 3 1172
天涯浪人
天涯浪人 2020-12-20 21:34

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

Default cursor results:

cursor = [
    {\'_id\': ObjectId(\'5220b974a61ad0000746c0d         


        
3条回答
  •  情书的邮戳
    2020-12-20 22:28

    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)
    

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

    Transforming Code into Beautiful, Idiomatic Python

提交回复
热议问题