How to aggregate by year-month-day on a different timezone

后端 未结 8 1671
无人及你
无人及你 2020-11-29 02:21

I have a MongoDB whom store the date objects in UTC. Well, I want to perform aggregation by year,month day in a different timezone (CET).

doing this, works fine for

8条回答
  •  离开以前
    2020-11-29 03:00

    The solution with timezone is a good one, but in version 3.6 you can also format the output using timezone, so, you get the result ready for use:

    {
    "$project":{
        "year_month_day": {"$dateToString": { "format": "%Y-%m-%d", "date": "$tDate", "timezone": "America/Chicago"}}
    },
    "$group":{
        "_id": "$year_month_day",
        "count":{"$sum":1}
    }
    }
    

    Make sure that your "$match" also considers timezone, or else you will get wrong results.

提交回复
热议问题