MongoDB ObjectId Data Type – Guidelines

Today in this article, we will see more details about MongoDB ObjectId Data type.

ObjectId is the 12-byte element consisting of a timestamp value, a random value, and an incremental counter.

Today in this article, we will cover below aspects,

MongoDB _id field as ObjectId

MongoDB uses the _id field as ObjectId this Id has few behavioral characteristics and it is explained below, A 4-byte timestamp is a UNIX timestamp.

  • MongoDB database automatically generates _id the field for insertion.
  • This field is unique for every document inserted into the collection.
  • _id field is automatically indexed and the index is unique.

It represents the ObjectId’s creation, measured in seconds since the Unix epoch. for more details, please visit here.

MongoDB ObjectId fields details

ObjectId is the 12-byte element consisting of a timestamp value, a random value, and an incremental counter.

MongoDB ObjectId

  • A 4-byte timestamp is a UNIX timestamp. It represents the ObjectId’s creation timestamp, measured in seconds since the Unix epoch.

If interested to know how to get timestamp using these 4 bytes, please visit the below article,

  • A 5-byte random value is generated once per process. This random value is unique to the machine and process.

  • A 3-byte incrementing counter, initialized to a random value.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *