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

时间:2022-10-22 17:04:01

I'm thinking that UTC time strings like 2011-01-26 21:41:09 +0000 might 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)将使文档更具可读性。从可读性的角度来看,将日期转换为一个历元整数似乎是最不吸引人的,但也许这对性能来说是最好的(或者这对性能有影响吗?)这里推荐的练习是什么?

5 个解决方案

#1


29  

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.

时间是一维的。时间戳加上时区是二维的,描述时间点和位置。沙发视图是一维的(但不是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"

#2


6  

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.

如果你只是使用地图的地图一面减少,这些建议可能是好的。但是,如果您希望对结果(_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('-')),那么您可以使用group_level=3、2和1分别返回_count(例如)每天、月和年。

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"的所有tweet总数,以及该用户的每日、每月和年度统计数据。

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

如果您不使用reduce部分,那么基于字符串的键很可能没问题。

#3


5  

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.

对于brevety,我更喜欢常规的“秒since epoch”方法,而不是“毫秒since epoch”。

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

数学。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.

至于易读性,我想将它存储为整数,以便于比较,并使用前端来很好地显示它。

#4


4  

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

这里有一个很好的解决方案:在CouchDB视图中排序日期

#5


3  

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之外的数据时是这样。

#1


29  

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.

时间是一维的。时间戳加上时区是二维的,描述时间点和位置。沙发视图是一维的(但不是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"

#2


6  

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.

如果你只是使用地图的地图一面减少,这些建议可能是好的。但是,如果您希望对结果(_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('-')),那么您可以使用group_level=3、2和1分别返回_count(例如)每天、月和年。

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"的所有tweet总数,以及该用户的每日、每月和年度统计数据。

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

如果您不使用reduce部分,那么基于字符串的键很可能没问题。

#3


5  

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.

对于brevety,我更喜欢常规的“秒since epoch”方法,而不是“毫秒since epoch”。

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

数学。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.

至于易读性,我想将它存储为整数,以便于比较,并使用前端来很好地显示它。

#4


4  

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

这里有一个很好的解决方案:在CouchDB视图中排序日期

#5


3  

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之外的数据时是这样。