Follow Us on Twitter

The MongoDB ObjectId explained in under 2 minutes

by Patrick Sinke on April 16, 2018 · 7 comments

As you may have noticed, especially when your background is from a MySQL or Oracle database, is that the unique identifier for database records (or ‘documents’ in MongoDB) is quite different. It’s not a incremental counter, but a long string of characters. 12 bytes in hexadecimal format, to be precise. And although they appear to contain no information, they actually do. This is how an ObjectId is composed.

Let’s take an ObjectId:

507f1f77bcf86cd799439011

The first part is a 4-byte value representing the seconds since the Unix epoch (hence, it’s a timestamp!), the next part is a 3-byte machine identifier unique for every machine, then a 2-byte containing the process id, and finally a 3-byte counter, starting with a random value.

This combination of values (timestamp, machine, process, counter) guarantees a unique value for the ObjectId.

Because the timestamp is included, it’s possible to extract it from the Id. That’s fairly simple, because Mongo provides a function for that:

ObjectId("507c7f79bcf86cd7994f6c0e").getTimestamp()

returns
ISODate("2012-10-15T21:26:17Z")

Just as simple is extracting the stringvalue:

ObjectId.valueOf(("507c7f79bcf86cd7994f6c0e")
 returns 507c7f79bcf86cd7994f6c0e See the MongoDB reference for more examples.

 

Ratings:
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

{ 7 comments… read them below or add one }

Connect Dell E310DW Printer to wifi July 29, 2019 at 1:56 pm

I read this article and knew the new things. this is very interesting seriously. I want to know more about this topic from this website

Reply

Online College Writing Services November 26, 2019 at 9:33 am

A Custom Writing Company should offer legitimate College Research Paper Services, Cheap Custom Research Paper Services and high quality Custom College Paper Services.

Reply

Error Code 0xc0000225 January 17, 2020 at 10:51 am

Wow! what an idea ! Great concept ! Beautiful ..
Your post was very nice……………

Reply

Medical Assignment Writing Services June 23, 2020 at 2:49 pm

Medical research paper writing services seekers have been on the rise lately since most learners need Medical Assignment Writing Services, medicine essay writing help and nursing essay writing services.

Reply

Business Essay Writing Services July 1, 2020 at 2:54 pm

Business management coursework writing service learners have an advantage of hiring the best business research writing service that is familiar with business term paper writing services for their Business Essay Writing Services.

Reply

Finance Coursework Writing Services July 8, 2020 at 7:41 am

There are many custom finance assignment writing services and Finance Coursework Writing Services to choose from for those stuck with their finance case study writing services and finance research paper help services.

Reply

Lego fan August 13, 2020 at 11:00 pm

If you interested in creative Lego builds or you collect own favorite Lego series, welcome to our Lego fan blog where you find latest news on Lego and more.

Reply

Leave a Comment

 

Previous post:

Next post:

About Whitehorses
Company profile
Services
Technology

Whitehorses website

Home page
Whitebooks
Jobs

Follow us
Blog post RSS
Comment RSS
Twitter