Mongodb aggregate: convert date to another timezone

前端 未结 2 1178
失恋的感觉
失恋的感觉 2020-12-16 01:13

I save my transaction with something like :

{code: \"A\", total: 250000, timestamp: ISODate(\"2016-01-20T23:57:05.771Z\")},
{code: \"B\", total: 300000, time         


        
2条回答
  •  鱼传尺愫
    2020-12-16 01:22

    MongoDB 3.6 added timezone parameter to the date manipulation operators. See Kevin's answer.

    We can add the "timestamp" to 7 * 60 * 60 * 1000 in a $project stage.

    The following pipeline seems to work in MongoDB 3.4 or older.

    db.collection.aggregate([
        { "$project": {
            "year": { "$year": { "$add": [ "$timestamp", 7 * 60 * 60 * 1000 ] } }, 
            "month": { "$month": { "$add": [ "$timestamp", 7 * 60 * 60 * 1000 ] } }, 
            "day": { "$dayOfMonth": { "$add": [ "$timestamp", 7 * 60 * 60 * 1000 ] } } 
        } }
    ])
    

提交回复
热议问题