Question

So here is the problem: I'm using MongoDB in my project so there are 24-characters ObjectId, using only hexadecimal alphabet. I'm make http request in my project to a provider, in this request I need to put a unique Id for callbacks purpose, but the provider allows only 20 characters for this id, and I don't know why.

So, my question is, with a 16 characters alphabet (hexa), there are : 16^24 possible mongo Ids, right ? Supposing I use in the HTTP request an Id based on 64 different characters ([0-9][a-z][A-Z]-_), correct me if I'm wrong but I think there are 64^20 possible Ids. So technically, it is possible to encode every possible MongoDB ObjectId with a corresponding Id, isn't it ?

It seems to be a classic Base64 encoding but mysteriously this does not work as I expected, I think I didn't understand how Base64 encoding works because the generated strings are bigger than original strings...

Do you think all of this is even possible or did I totally miss something ?

Thanks in advance!

EDIT: One of my colleague tried something which seems to work. Here is the Java code :

byte[] decodedHex = Hex.decodeHex("53884594e4b0695f366f8128".toCharArray());
byte[] encodedHexB64 = Base64.encodeBase64(decodedHex);
System.out.println(new String(encodedHexB64)); // --> U4hFlOSwaV82b4Eo

For a reason that I ignore, doing this is not the same:

String anotherB64 = Base64.encodeBase64String("53884594e4b0695f366f8128".getBytes());
System.out.println(anotherB64);

And it prints : NTM4ODQ1OTRlNGIwNjk1ZjM2NmY4MTI4

Was it helpful?

Solution

MongoDB is using ObjectId as a default primary key for the documents because it's fast to generate and very likely to be unique.

But you are not forced to use it as a primary key. You can use any BSON data type in the _id field as long is not an array. That being said, you can use your 20-char Id in _id field.

EDIT:

From your original question I didn't know that you're using an existing DB. The _id field is immutable and it cannot be changed in an existing document.

If you only wanted to convert the existing ObjectId to something else that's 20 chars long the method you posted will work.

The second method produces a long string because you're basically base64 encoding a string which will produce an even longer string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top