javascript - How to get timestamp using Object id in mongoDB - Stack Overflow

how can I get timestamp using object id in MongoDB.I am using NodeJS and mongoDB as my backend service

how can I get timestamp using object id in MongoDB.I am using NodeJS and mongoDB as my backend service I want to get the date and time of the document when it is inserted in the collection. This is what i am using :

 db.Users.find()._id.getTimestamp()

OUTPUT

 ISODate("2018-09-11T11:00:38Z")

Now, what I want to know is:

1) Separate date and time from above string and convert it into readable format.

2) This timestamp returned by MongoDB object id is based on server time or user's puter.

THANKS

how can I get timestamp using object id in MongoDB.I am using NodeJS and mongoDB as my backend service I want to get the date and time of the document when it is inserted in the collection. This is what i am using :

 db.Users.find()._id.getTimestamp()

OUTPUT

 ISODate("2018-09-11T11:00:38Z")

Now, what I want to know is:

1) Separate date and time from above string and convert it into readable format.

2) This timestamp returned by MongoDB object id is based on server time or user's puter.

THANKS

Share Improve this question edited Sep 30, 2021 at 9:30 Yilmaz 50k18 gold badges218 silver badges271 bronze badges asked Sep 23, 2018 at 15:41 Digvijay Singh ThakurDigvijay Singh Thakur 3,2916 gold badges46 silver badges109 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

1) getTimestamp() is available on ObjectID objects, like the _id field by default. So however you are getting your documents will have it available. This returns a javascript Date object, so its easy to extract and manipulate it how you need.

2) The embedded timestamp in the ObjectID is dependent on where it was created. If it was created client-side, it'll be from the client system; if it was created server-side, it'll be from the server system.

1-

.getTimestamp().toLocaleDateString() //get the date
  .getTimestamp().toLocaleTimeString() // get the time

2-Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970). One standard way that many databases store dates is as a count of milliseconds since the Epoch, with 0 representing January 1, 1970 at 00:00:00GMT. This is how dates are stored internally in most programming languages.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744325352a4568623.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信