简体   繁体   English

MongoDB-存储毫秒而不是日期

[英]MongoDB - Store millisecond instead of Date

I'm using PHP as server side language. 我使用PHP作为服务器端语言。 I'm getting date in millisecond from my ajax request and converted it into MongoDate, then i store it into MongoDB. 我从我的ajax请求中获取了以毫秒为单位的日期,并将其转换为MongoDate,然后将其存储到MongoDB中。 Like below. 像下面。

$date = new MongoDate($millisecond / 1000);
$db->test->insert({"date": $date});

Again, when i need date in my javascript, i'm getting MongoDate and and converted into millisecond and i pass it to my UI. 再次,当我需要在JavaScript中使用日期时,我得到了MongoDate并转换为毫秒,然后将其传递给我的UI。 Like below 像下面

foreach($cursor as $row) {
    $row["date"] = $row["date"]->sec * 1000;
}

Instead, why should i not store date as millisecond instead of Dateobject? 相反,为什么我不应该将日期而不是Dateobject存储为毫秒? Still i can compare two dates, even-though i store it as millisecond. 尽管我将其存储为毫秒,但我仍然可以比较两个日期。

Any other disadvantages would i face in future, while i do reports with date as millisecond? 我将来以毫秒为单位进行报告时,将来还会遇到其他不利条件吗?

Currently the only place I can think of where it would matter if a date field were actually stored as the ISODate type is within the aggregation framework, however, I have no doubt that such a constraint will eventually change when casting becomes possible (this I would like to see). 目前,我唯一能想到的是,是否将日期字段实际存储为ISODate类型会在聚合框架内发生什么事,但是,我毫无疑问的是,这种约束最终将在铸造成为可能时改变(这是我会希望看到)。

Other than that your value is easily usable within both PHP/any other language and Map Reduces own date constructs. 除此之外,您的价值很容易在PHP /任何其他语言和Map Reduces自己的日期构造中使用。 With this in mind I see no problem if you don't require the aggregation frameworks date operators. 考虑到这一点,如果您不需要聚合框架日期运算符,我认为不会有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM