Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

node.js - 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 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...