json 在 CouchDB 中存储日期时间(时间戳)的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4812235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:43:12  来源:igfitidea点击:

What's the best way to store datetimes (timestamps) in CouchDB?

jsondatetimecouchdb

提问by dan

I'm thinking that UTC time strings like 2011-01-26 21:41:09 +0000might be okay since they sort correctly if they are used in a view key, but storing the time zone (e.g. 2011-01-26 16:41:09 -0500) would make the document more readable. Converting the date into an epoch integer seem the least appealing from a readability standpoint, but maybe best for performance (or does it make a difference?). What's the recommended practice here?

我认为 UTC 时间字符串2011-01-26 21:41:09 +0000可能没问题,因为如果它们在视图键中使用它们可以正确排序,但是存储时区(例如2011-01-26 16:41:09 -0500)会使文档更具可读性。从可读性的角度来看,将日期转换为纪元整数似乎最不吸引人,但可能对性能最好(或者它有什么不同?)。这里推荐的做法是什么?

回答by JasonSmith

Time is a one-dimensional thing. A timestamp plus timezone is two-dimensional, describing a point in time, and a location. Couch views are one-dimensional (but not the GeoCouch plugin), so storing in a common zone (UTC) is wise.

时间是一维的东西。时间戳加时区是二维的,描述时间点和位置。Couch 视图是一维的(但不是 GeoCouch 插件),因此存储在公共区域 (UTC) 中是明智的。

Probably the most future-proof format is a string that naturally sorts in chronological order. Probably the most convenient such format is what JSON2 outputs:

可能最适合未来的格式是按时间顺序自然排序的字符串。可能最方便的此类格式是 JSON2 输出的格式:

> a = new Date();
Thu Jan 27 2011 18:40:52 GMT+0700 (ICT)
> JSON.stringify(a)
"2011-01-27T11:40:52.280Z"

回答by BigBlueHat

If you're just using the Map side of Map reduce than these suggestions are probably fine. If, however, you want to do a reduce on the results (_count, _stats, _sum), then I'd recommend emitting your dates as arrays so you can use group_level.

如果您只是使用 Map reduce 的 Map 端,那么这些建议可能没问题。但是,如果您想减少结果(_count、_stats、_sum),那么我建议您将日期作为数组发出,以便您可以使用 group_level。

For instance, if you emit(doc.date.split('-')) on a date strings formatted like "2011-02-14", then you could return _count's (for instance) per day, month, and year by using group_level=3, 2, and 1 respectively.

例如,如果您在格式为“2011-02-14”的日期字符串上发出(doc.date.split('-')),那么您可以通过使用每天,每月和每年返回_count(例如) group_level 分别为 3、2 和 1。

You can further filter the data by adding non-date data to the beginning of the key. If you were outputting Twitter names, for instance, your key might look like ["bigbluehat", "2011", "02", "14"] and your reduce could return the total count of all tweets for the user "bigbluehat" as well as stats for that user across day, month, and year.

您可以通过在键的开头添加非日期数据来进一步过滤数据。例如,如果您要输出 Twitter 名称,则您的键可能看起来像 ["bigbluehat", "2011", "02", "14"] 并且您的 reduce 可以将用户“bigbluehat”的所有推文总数返回为以及该用户跨日、月和年的统计数据。

If you're not using the reduce side of things, then string-based keys are likely fine.

如果您没有使用 reduce 方面的东西,那么基于字符串的键可能没问题。

回答by Kristian

No matter what kind of data storage I use, I typically want a unix timestamp in there as a field, in which I'll include one for the created date, and then an updated field that I can change when the document changes.

无论我使用哪种数据存储,我通常都希望在其中包含一个 unix 时间戳作为一个字段,其中我将包含一个用于创建日期的字段,然后是一个可以在文档更改时更改的更新字段。

I prefer the regular "seconds since epoch" approach rather than "milliseconds since epoch" simply for brevety.

为了简洁起见,我更喜欢常规的“自纪元以来的秒数”方法而不是“自纪元以来的毫秒数”。

Math.round(new Date().getTime()/1000)does the trick for me.

Math.round(new Date().getTime()/1000)对我有用。

In terms of readibility, i want to store it as an integer for easy comparisons, and use the front end to display it nicely.

在可读性方面,我想将它存储为一个整数以便于比较,并使用前端来很好地显示它。

回答by geoffreyd

You can store your dates how ever you want*, it is how you output them into your views that is important.

您可以随心所欲地存储日期*,重要的是将它们输出到视图中的方式。

*As long as Date.parse() can read it.

*只要 Date.parse() 可以读取它。

There is a good solution here: Sorting Dates in CouchDB Views

这里有一个很好的解决方案:Sorting Dates in CouchDB Views

回答by Teddy

I like to use milliseconds since last epoch. You can figure this out with:

我喜欢使用自上一个纪元以来的毫秒数。您可以通过以下方式解决此问题:

new Date().valueOf()

You can create a new date from milliseconds with:

您可以从毫秒创建一个新日期:

var milliseconds = new Date().valueOf();
var date = new Date(milliseconds);

I like to create a view where the timestamp (in milliseconds) is the key b/c sorting is super-easy that way.

我喜欢创建一个视图,其中时间戳(以毫秒为单位)是关键 b/c 排序方式非常简单。

Also, I think using integers is more efficient than strings, at least when it comes to working with data outside of CouchDB.

另外,我认为使用整数比字符串更有效,至少在处理 CouchDB 之外的数据时是这样。