转换存储在 Mongo 集合中的时间戳
我在 Mongo 集合中存储了各种时间戳,有些作为浮点数,有些作为整数。
它们都存储在 BST 中,服务器很快就会切换到 UTC。如何在 Mongo 中将它们转换为 UTC 时间戳?
在 MySQL 中我可以这样做:
UPDATE `table` SET `field` = CONVERT_TZ(`field`, 'Europe/London', 'UTC');
是否有 Mongo 等效项?
I have various timestamps stored in Mongo collections, some as floats and some as ints.
They are all stored in BST and the server will be switched to UTC soon. How do I convert them within Mongo to be UTC timestamps?
In MySQL I can do this:
UPDATE `table` SET `field` = CONVERT_TZ(`field`, 'Europe/London', 'UTC');
Is there a Mongo equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用您选择的语言并一次更新一种。它应该像加载数据并重写数据的
for
循环一样简单。只需仔细检查您选择的语言如何处理跨时区的时间戳即可。进行此类数据更改可能会对生产代码产生各种意想不到的影响。
You'll have to use your language of choice and update them one at a time. It should be as simple as a
for
loop that loads the data and rewrites it.Just double-check how your language of choice handles timestamps across timezones. Making such a data change can have all kinds of unexpected effects on production code.
时间戳通常采用 UTC,而不是特定时区。我使用过的所有日期/时间库都会返回时间戳,即自 1970 年 1 月 1 日 UTC 以来的秒数(或毫秒数)。请检查用于创建时间戳的库的文档以确保确定。
这意味着您应该没问题,除非您使用了不遵循此约定的日期/时间库,或者以某种方式自己计算了时间戳并考虑了时区。
例如,在 JavaScript 中,如果您存储从
new Date().getTime()
返回的值,然后将该值传递给不同系统上的new Date(...)
无论两个系统的时区如何,您最终都会得到相同的绝对日期/时间。 Ruby 也是如此,在一台机器上执行Time.new.to_i
,然后在另一台机器上运行Time.at(...)
,您将获得相同的绝对日期/时间。当我说“绝对日期/时间”时,我的意思是 UTC 时间是相同的,系统很可能会在本地时区中显示它,但这就是您想要的。Timestamps are generally in UTC and not in a specific timezone. All date/time libraries I've worked with return timestamps that are the number of seconds (or milliseconds) since Jan 1st 1970 UTC. Check the documentation for the library you used to create the timestamp to be sure.
This means that you should be ok, unless you have used a date/time library that does not follow this convention, or somehow calculated the timestamps yourself and accounted for the timezone.
For example in JavaScript if you store the value returned from
new Date().getTime()
and later pass that value tonew Date(...)
on a different system you will end up with the same absolute date/time, regardless of the timezones of the two systems. The same goes for Ruby, doTime.new.to_i
on one machine, and runTime.at(...)
on another and you will get the same absolute date/time. When I say "absolute date/time" I mean that it's UTC time will be the same, most likely the system will display it in the local time zone, but that is what you want.关于 Mongo 中的日期需要考虑的一些要点:
自 1970-01-01T00:00:00Z 以来的毫秒数
在由驱动程序存储在 MongoDB 中之前将转换为 UTC
建议不要使用 DateTime.Parse。
您将遇到有关日期时间格式的各种时区问题。
相反,只需使用具有 UTC 风格的 DateTime 构造函数之一。
示例:
希望您觉得它有用。
Some points to consider about date in Mongo:
milliseconds since 1970-01-01T00:00:00Z
will be converted to UTC before being stored in MongoDB by the driver
The recommendation is not to use DateTime.Parse.
You will get into all sorts timezone issues regarding how DateTimes are formatted.
Instead just use one of the DateTime constructors with UTC flavor.
Example:
Hope you find it useful.