Can I get more granularity from timestamp MongoDB?

I get the timestamp from my MongoDB database, but it returns with precision only in seconds:

Thu Jun 06 2013 16:15:22 GMT-0400 (Eastern Daylight Time)

I need a lot of detail. Is there a way to get the MongoDB timestamp with millisecond precision?

I am calling my node.js server through mongoose:

var timestamp = new ObjectID().getTimestamp(); 

How can I get something more accurate?

+6
source share
2 answers

ObjectIds are stored to the exact second and you cannot change it. So if you need something from mill detailing, you have to keep your own timestamp value.

+6
source

I created timestamps one by one and got them:

 ObjectId("586a291570b9a181fc2f61ba").getTimestamp(); ISODate("2017-01-02T10:19:01Z") 

The following timestamp:

 ObjectId("586a291570b9a181fc2f61bc").getTimestamp(); ISODate("2017-01-02T10:19:01Z") 

You will notice that although the only granularity printed by getTimestamp() is in seconds, there is another granularity that causes the ObjectId line to change, although both are at 01 second.

Reason :

 BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type. Timestamp values are a 64 bit value where: the first 32 bits are a time_t value (seconds since the Unix epoch) the second 32 bits are an incrementing ordinal for operations within a given second. Within a single mongod instance, timestamp values are always unique. 

Thus, the true granularity refers to the 32-bit order created by MongoDB. It is associated with operations within a second , not the time value, so you do not get it in milliseconds.

0
source

Source: https://habr.com/ru/post/946727/


All Articles