Find creation timestamp of document from ObjectId

Problem Statement

We have below collection named student, there is no field for creation time of document. But we have to find out creation timestamp of all documents.

Collection Name: students

[
  {X
    _id: ObjectId('6626fb9e6548c947e716c9b7'),
    name: 'Joe Rogan',
    age: 35,
    country: 'USA'
  },
  {
    _id: ObjectId('6626fe516548c947e716c9b9'),
    name: 'Chris Williamson',
    age: 35.4,
    country: 'Austrelia'
  },
  {
    _id: ObjectId('6626fe666548c947e716c9ba'),
    name: 'Lex',
    age: '28',
    country: 'USA'
  }
] 

To learn more about ObjectId see : All about ObjectId in MongoDB.

Solution

db.students.find({},{}).forEach(doc=>print("name : "+doc.name+" createdTime : "+doc._id.getTimestamp()))