DateTime对象和YYYY-MM-DD字符串之间的MongoDB区别 [英] MongoDB difference between DateTime object and YYYY-MM-DD string

查看:100
本文介绍了DateTime对象和YYYY-MM-DD字符串之间的MongoDB区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDB中,我只需要进行日期范围查询。但是数据集很大(9 M),并且将字符串覆盖到DateTime对象(我使用Perl脚本),然后将其插入MongoDB非常耗时。如果我仅将日期存储为字符串 YYYY-MM-DD,则范围查询gt: 2013-06-01和lt: 2013-08-31仍不会像给出日期时间类型?在这种情况下,它们是否相同?如果是这样,将其存储为DateTime对象的好处是什么。
谢谢。

In MongoDB, I only need to make date range queries. But the data set is huge (9 M) and coverting a string to DateTime object (I use Perl script) and then inserting them into MongoDB is very time consuming. If I just store the dates as strings "YYYY-MM-DD", would not the range query gt:"2013-06-01" and lt:"2013-08-31" still give me the same results as if they were of datetime type? Are they the same in this scenario? If so, what would be the advantage of storing as a DateTime object. Thanks.

推荐答案

如果您不关心应用程序中的时区支持,则可以使用字符串MongoDB中的基本查询应该可以正常工作(但是,如果有关系,您将需要一个真正的 Date 类型)。

If you don't care about time-zone support in your application, then using strings for basic queries in MongoDB should work fine (but if it does matter, you'll want a real Date type).

但是,如果以后要进行日期数学运算或将Aggregation Framework与date字段一起使用,则该字段实际上必须是 Date 类型:

However, if you later want to do date math or use the Aggregation Framework with your date field, it's necessary that the field is actually a Date type:

http://docs.mongodb .org / manual / reference / aggregation /#date-operators

例如,您可以使用 $ dayOfWeek Date 类型字段上的c $ c>函数。

For example, you could use the $dayOfWeek function on the Date typed field.

您可能会做一些简单的事情,例如按年份分组通过使用 $ substr doc ),但生成的代码将与清晰(也不太可能执行)。

You could likely do some simple things like group on year by using $substr (doc) in MongoDB, but the resulting code will not be as clear (nor likely perform as well).

虽然差别不大,但建议尽可能将它们存储为 Date 类型。

While it's not a huge difference, I'd recommend storing them as Date types if possible generally.

我在 docs 的Perl驱动程序,警告开发人员不要使用 DateTime ,因为它的运行速度很慢,因此,如果您定期使用Perl,并且聚合框架不是一个大问题,最好将它们存储为数字或字符串,并在Perl中根据需要进行转换。

I see in the docs for the Perl driver that developers are warned against using the DateTime due to the fact that it is very slow, so maybe if you use Perl regularly, and the Aggregation Framework isn't a big issue, you'd be better off storing them as either numbers or as strings, and converting them as needed in Perl.

如果空间不足,请删除不必要的字符(例如-):

If space is an issue, remove unnecessary characters (such as the -):

20130613 -> 
    4 bytes for length of string 
    8 bytes encoded as UTF-8
    NULL character

应该是13个字符。另一方面, BSON / MongoDB中的DateTime值需要8个字节(Perl <$ c $也会如此) c> $ time 函数)。

That would be 13 characters. A DateTime value in BSON/MongoDB requires 8 bytes on the other hand (as would the Perl $time function).

(我强烈建议您进行一些性能测试,以了解是否会对性能产生影响在带有Perl的MongoDB中使用 Date 类型将影响您的典型工作流程。)

(I'd strongly recommend you do a bit of performance testing to find out if the performance impact of using a Date type in MongoDB with Perl will impact your typical workflows.)

这篇关于DateTime对象和YYYY-MM-DD字符串之间的MongoDB区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆