Skip to content Skip to sidebar Skip to footer

Shorten Mongodb Id In Javascript

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

Solution 1:

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:

  • a 4-byte timestamp
  • a 3-byte machine identifier
  • a 2-byte process id
  • and a 3-byte counter.

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:

  • Remove normally significant bits -- if you do this, don't shard the collection
  • Devise your own your own unique ID solution (and if it's in a web-farm, it may end up looking very similar to MongoDB's to handle uniqueness)
  • use the ObjectId as a long number and run a shorten algorithm on it (it will need to be broken down first into smaller chunks as it exceeds JavaScript's numeric precision of 53 bits), try this algorithm for example = encode it (will end up around 17 characters)
  • use something else shorter, but unique as the Id for your documents
  • Easiest: Just accept that the Ids are long. :)

(It's not clear why the browser would need to do this conversion--why would it have the document's ObjectID?)

Solution 2:

In addition to answer from WiredPrairie,

You can change from hexadecimal representation to base64 representation. It'd represent 12 bytes in 16 characters from 24 in hexadecimal. It's not as good as 7 but it's a start. There's even a library to help with that -> base64-mongo-id.

5f20318f5b100c5f7ed80e44 would be shortened to XyAxj1sQDF9+2A5E or XyAxj1sQDF9-2A5E if you replace + and / with - and _.

But be aware if you use base 64, the URL will be case sensitive. If you want to avoid that you can rather use base36 which is case insensitive but would be longer i.e. 20 characters representing 5f20318f5b100c5f7ed80e44 as L4QDDD23CAGF67WYBZCA.

If you need shorter ones and if you can handle case sensitiveness and special characters in URL, you can use base85 i.e. ASCII to directly encode data or higher order encoding like base 91 and base 122 which reduce them to

Post a Comment for "Shorten Mongodb Id In Javascript"