Aggregate MongoDB results by ObjectId date

前端 未结 3 1167
天涯浪人
天涯浪人 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"}
                    }
                }
            }
    }
    

提交回复
热议问题