ObjectId is twice as long as the actual stored data

  mongodb, question

ObjectId is twice as long as the actual stored data. How do you understand that, and how many bytes does this primary key take up? Is it too long

I guess that’s what you might mean. Here is oneObjectId:

ObjectId("5a6940702c4df0af27ffc4ed")

If you count the inside of quotation marks, the length is 24. But this is not the actual length of storage, but the hexadecimal representation of the actual data stored. If you don’t know what this means, you may want to recall the university courses. 1 byte representation:

  • Binary: 00000000~11111111
  • Decimal: 0~255
  • Hexadecimal: 00~ff.

So the hexadecimal representation of 12 bytes is 24 characters. But in fact it only has 12 bytes, and the storage only takes up 12 bytes.
AboutObjectIdRefer to the documentation for the specific composition of:ObjectId.
As for whether it is too long … what is the standard for too long?