保存我的交易的东西,如:MongoDB的骨料:日期转换到另一个时区
{code: "A", total: 250000, timestamp: ISODate("2016-01-20T23:57:05.771Z")},
{code: "B", total: 300000, timestamp: ISODate("2016-01-20T05:57:05.771Z")}
交易的每个人都有下它UTC时区timestamp
场。由于我住在雅加达(UTC + 7)时区,因此我需要在聚合之前将7小时添加到我的时间戳。这里是我的蒙戈语法:
db.transaction.aggregate(
[
{
$project:
{
year: { $year: "$timestamp" },
month: { $month: "$timestamp" },
day: { $dayOfMonth: "$timestamp" }
}
}
])
它返回:
{
"_id" : ObjectId("56a01ed143f2fd071793d63b"),
"year" : 2016,
"month" : 1,
"day" : 20
},
{
"_id" : ObjectId("56a01ed143f2fd071793d63b"),
"year" : 2016,
"month" : 1,
"day" : 20
}
这是因为第一个事务错误(代码A
),是发生在1月21日,但因为它被转换为UTC(-7小时),它成为ISODate("2016-01-20T23:57:05.771Z")
注:我知道about the same problem over here,这里就是我一直到目前为止已经试过:
db.transaction.aggregate(
[
{$project: {"timestamp": {$add: [7 * 60 * 60 * 1000]}}},
{
$project:
{
year: { $year: "$timestamp" },
month: { $month: "$timestamp" },
day: { $dayOfMonth: "$timestamp" }
}
}
])
但它返回can't convert from BSON type NumberDouble to Date
错误。 有什么建议吗?
当您只有一个时区但不适用于动态时区时,这是一个很好的解决方案。 –