Aggregate MongoDB results by ObjectId date

前端 未结 3 1154
天涯浪人
天涯浪人 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:15

    The Jira Ticket pointed out by llovett has been solved, so now you can use date operators like $isoWeek and $year to extract this information from an ObjectId.

    Your aggregation would look something like this:

    {
        "$project":
            {
    
                "_id": {
                    "$dateFromParts" : {
                        "year": { "$year": "$_id"},
                        "month": { "$month": "$_id"},
                        "day": { "$dayOfMonth": "$_id"}
                    }
                }
            }
    }
    
    0 讨论(0)
  • 2020-12-20 22:27

    There is no way to accomplish what you're asking with mongodb's aggregation framework, because there is no aggregation operator that can turn ObjectId's into something date-like (there is a JIRA ticket, though). You should be able to accomplish what you want using map-reduce, however:

    // map function
    function domap() {
        // turn ObjectId --> ISODate
        var date = this._id.getTimestamp();
        // format the date however you want
        var year = date.getFullYear();
        var month = date.getMonth();
        var day = date.getDate();
    
        // yields date string as key, entire document as value
        emit(year+"-"+month+"-"+day, this);
    }
    
    // reduce function
    function doreduce(datestring, docs) {
        return {"date":datestring, "docs":docs};
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题