MongoDB中是否有NOW()的等效项 [英] Is there any Equivalent of NOW() in MongoDB

查看:87
本文介绍了MongoDB中是否有NOW()的等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试从MySQL迁移到MongoDB,并希望像在MySQL中使用NOW()一样插入当前日期.有谁知道一线解决方案,还是我应该使用PHP?

I am trying to migrate from MySQL to MongoDB and want to insert the current date as I do with NOW() in MySQL. Does anyone know a 1 line solution or should I do it with PHP?

推荐答案

获取文档创建的时间戳记

如果您在_id字段中使用MongoDB的默认ObjectID ,则其中包括时间戳记您可以用来推断文档创建日期的组件. PHP驱动程序包括 MongoId :: getTimestamp()方法,该方法可用于提取时间通过一个ObjectID.

Getting timestamp of document creation

If you are using MongoDB's default ObjectIDs for your _id field, these include a timestamp component which you can use to infer the creation date for a document. The PHP driver includes a MongoId::getTimestamp() method that can be used to extract the time from an ObjectID.

请注意,ObjectID通常是由PHP驱动程序(不是MongoDB服务器)生成的,因此,如果时间戳的准确性很重要,则使时钟与ntpd或类似时钟保持同步非常重要.

Note that the ObjectID is normally generated by the PHP driver (not the MongoDB server) so it is important to have your clocks in sync with ntpd or similar if timestamp accuracy is important.

在即将发布的MongoDB 2.6版本中,添加了新的$currentDate更新修饰符,该修饰符支持设置服务器端日期和时间戳.有关更多信息,请参阅:MonaDB Jira问题跟踪器中的 SERVER-10911 .

In the upcoming MongoDB 2.6 release, a new $currentDate update modifier has been added which supports setting server-side dates and timestamps. For more information see: SERVER-10911 in the MongoDB Jira issue tracker.

在此服务器端支持可用之前,添加当前日期/时间的最佳方法是使用time()或等效功能在应用程序代码中进行设置.

Until this server-side support is available, your best approach for adding current date/time would be to set in your application code using time() or an equivalent function.

这篇关于MongoDB中是否有NOW()的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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