Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a MongoDB document with a timestamp from the database server

Tags:

java

mongodb

In Oracle, I could simply do this:

INSERT INTO myTable VALUES ('someValue',SYSTIMESTAMP);

That would insert two values into myTable, and one of them would be a timestamp based-on the database server's time.

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.

Is there a way to have MongoDB apply a timestamp to a document based-on the the database server time?

like image 882
Aaron Avatar asked May 27 '12 03:05

Aaron


1 Answers

A little late to the game - but the recent releases of mongodb have $currentDate.

See http://docs.mongodb.org/manual/reference/operator/update/currentDate/

like image 196
Shane Gannon Avatar answered Oct 02 '22 18:10

Shane Gannon