Tech Dive 1: MongoDB's ObjectId
Let's get to know about the ObjectId

Search for a command to run...
Let's get to know about the ObjectId

No comments yet. Be the first to comment.
React hooks were introduced in React 16.8 to use State and other React Features. It lets you hook into the features without needing to create Classes. 1. useState This might be the first hook you heard about when learning React. It is very common and...

So at this point I assume you should know How to create a Repository, If you don't do checkout my previous article here. Let's dive in and add files via the Command Line. Follow the steps 1, 2 and 3 from my previous article! Once done, You shoul...

As a software engineer, learning a version control tool is one of the best things you can have in your bag. Right off the bat, I want to clear this doubt many people have! Is Git and GitHub the same? The answer is NO! In order to explain it in sim...

MongoDB is a famous NoSQL Database. If you're just getting started with MongoDB, I would suggest you begin with Mongo University
MongoDB's ObjectID is made of 12 bytes. The 12 bytes consists of
a 4-byte timestamp.
a 5-byte random value generated once per process.
a 3-byte incrementing counter initialized to a random value.
Example:
65f87920068c7d17cb1288d6
65f87920 - Unix Epoch Timestamp
068c7d17cb - Random Value
1288d6 - Incrementing Counter
You can use the ObjectId() method to construct a new ObjectId.
Optionally, it can take two inputs.
hexadecimal - A 24-character hexadecimal string value.
integer - The integer value, in seconds, is added to the Unix epoch to create the new timestamp.
Example:
> ObjectId(10)
< ObjectId('0000000a0d098c2eda0f296e')

Since the timestamp is included in the ObjectId, you can retrieve the time in which the ObjectId was generated.
> ObjectId('665377378755af3d1e7c5701').getTimestamp()
< 2024-05-26T17:53:59.000Z
> ObjectId('0000000a0d098c2eda0f296e').getTimestamp()
< 1970-01-01T00:00:10.000Z
You can use this method in case if you want to convert the ObjectId to hexadecimal string.
> ObjectId('665b4d520d098c2eda0f296d').toString()
< 665b4d520d098c2eda0f296d
If you’re a tech enthusiast and resonate with my content, follow me on LinkedIn, GitHub, and Twitter.