How can I shorten the mongodb ID to a much easier to parse syntax for use in a URL. The string is way too long in it's current iteration.
Base64 is decent, but still too long. I'm looking for more in the sub-7 characters range.
I would like to be able to encode/decode it in both node.js and the browser.
Parsing the ObjectId from a request wouldn't be difficult (so I'm not sure why that is a problem?). If the goal is to make typeable URLs, then having a shorter and "friendlier" URL would be valuable.
You can't take a 12 byte number that is guaranteed unique in a sharded MongoDB setup and condense it to fewer than 12 bytes and have it be guaranteed unique (you mentioned under seven characters for example).
From the docs, the MongoDB ObjectId consists of:
So, you'll either need to sacrifice some portion of the ObjectId (and hence sharding), or devise an alternate Id creation format that is indexed.
While you could hash the ID potentially, again, conflicts can arise that you'd want to code for (again, you can't take 12 bytes down to 4 bytes and guarantee uniqueness). And if there are conflicts possible (and there will be if you reduce the total number of bits available), you'll need some sort of secondary table anyway (and you'd need to create an index to go from generated ID to ObjectId).
Resulting options:
(It's not clear why the browser would need to do this conversion--why would it have the document's ObjectID?)