如何从数据库服务器插入带有时间戳的MongoDB文档 [英] How to insert a MongoDB document with a timestamp from the database server
问题描述
在Oracle中,我可以这样做:
In Oracle, I could simply do this:
INSERT INTO myTable VALUES ('someValue',SYSTIMESTAMP);
这会在myTable中插入两个值,其中一个是基于数据库的时间戳服务器的时间。
That would insert two values into myTable, and one of them would be a timestamp based-on the database server's time.
对于MongoDB(通过Java驱动程序)我试过这个:
For MongoDB (via the Java driver) I've tried this:
myDoc.put("value","someValue");
myDoc.put("timestamp", new Date());
myCollection.insert(myDoc);
但这会根据客户端计算机的时间创建时间戳,而不是数据库服务器的时间。
But that creates a timestamp based-on the client machine's time, not the database server's time.
有没有办法让MongoDB根据数据库服务器时间对文档应用时间戳?
Is there a way to have MongoDB apply a timestamp to a document based-on the the database server time?
推荐答案
游戏有点晚了 - 但最近发布的mongodb有$ currentDate。
A little late to the game - but the recent releases of mongodb have $currentDate.
请参阅 http: //docs.mongodb.org/manual/reference/operator/update/currentDate/
这篇关于如何从数据库服务器插入带有时间戳的MongoDB文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!