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

后端 未结 8 1675
无人及你
无人及你 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 02:54

    After searching for hours, this is the solution that worked for me. It is also very simple. Just convert the timezone by subtracting the timezone offset in milliseconds.

    25200000 = 7 hour offset // 420 min * 60 sec * 1000 mili

    $group: {
        _id = { 
            year: { $year : [{ $subtract: [ "$timestamp", 25200000 ]}] }, 
            month: { $month : [{ $subtract: [ "$timestamp", 25200000 ]}] }, 
            day: { $dayOfMonth : [{ $subtract: [ "$timestamp", 25200000 ]}] }
        },
        count = { 
            $sum : 1
        }
    };
    

提交回复
热议问题