Skip to content
Advertisement

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

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?

Advertisement

Answer

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

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement