Is it possible to decript ObjectId in partitions from string?

I know about ObjectId how it works internally and its bytes occupation from both docs: bson-types/#objectid and method-ObjectId,

Is it possible to decript like below without using any mongodb functions?

const ObjectID = require("mongodb").ObjectID;

var id          = new ObjectID().toString(), ctr = 0;
var timestamp   = id.slice(ctr, (ctr+=8));
var machineID   = id.slice(ctr, (ctr+=6));
var processID   = id.slice(ctr, (ctr+=4));
var counter     = id.slice(ctr, (ctr+=6));
console.log("id:", id);
console.log("timestamp:", timestamp);
console.log("machineID:", machineID);
console.log("processID:", processID);
console.log("counter:", counter);  


id: 607c3511f6c1fb54f91046da
timestamp: 607c3511
machineID: f6c1fb
processID: 54f9
counter: 1046da

And convert that parts in Integer:

console.log("timestamp:", parseInt(timestamp, 16));
console.log("machineID:", parseInt(machineID, 16));
console.log("processID:", parseInt(processID, 16));
console.log("counter:", parseInt(counter, 16));  


timestamp: 1618752785
machineID: 16171515
processID: 21753
counter: 1066714

I am sure this is not a valid process to decript objectID, please suggest is there any way to get accurate partition?

Hi @turivishal,

Assuming your driver is generating ObjectIDs in this format (there are some historical variations), you can decode as you’ve suggested without using any MongoDB functions.

For example, as per your documentation links the middle 5 bytes are expected to be a random value. Some drivers used to compose this from a machineID and processID (per your interpretation). The 4-byte timestamp prefix is generally the only reliable information to decode; the rest of the bytes help with uniqueness for independently generated ObjectIDs.

Did any of your results differ from what was expected? If so, what specific driver & version are you using to create the ObjectIDs?


1 Like

Thank you for your reply,

I have not implemented yet, before that i just want to confirm if its accurate at-least 90% then i can think about it,

I have project in NodeJS, and i am using mongoose npm latest version, and i think mongoose using MongoDB NodeJS Driver,