MongoDB ObjectId timestamp

You need to record the creation time for each document in a MongoDB collection. Your first thought may be to store this information in a createdAt column, but there’s no need.

The MongoDB-generated document ID includes an embedded timestamp. You can verify this by running the following command in the mongo shell.

ObjectId().getTimestamp()

// ISODate("2020-07-28T16:48:35Z")

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.