MongoDB ObjectID 12 字节如何? [英] How is a MongoDB ObjectID 12 bytes?

查看:60
本文介绍了MongoDB ObjectID 12 字节如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,MongoDB 中的字符串以 UTF-8 格式存储,因此每个字符在 1 到 4 个字节之间.

From what I know, Strings in MongoDB are stored in UTF-8, so each character is between 1 and 4 bytes.

MongoDB 文档对 ObjectID 做了以下说明:

返回一个新的 ObjectId 值.12 字节的 ObjectId 值包括:

Returns a new ObjectId value. The 12-byte ObjectId value consists of:

一个 4 字节的值,表示自 Unix 纪元以来的秒数,
一个 5 字节的随机值,以及
一个 3 字节的计数器,从一个随机值开始.

a 4-byte value representing the seconds since the Unix epoch,
a 5-byte random value, and
a 3-byte counter, starting with a random value.

在一个例子中,它显示了 ObjectId("507f1f77bcf86cd799439011").这个字符串是 24 个字节,虽然在 UTF-8 中,所以我不明白 12 个字节在哪里发挥作用.

In an example, it shows ObjectId("507f1f77bcf86cd799439011"). This String is 24 bytes though in UTF-8, so I don't understand where the 12 bytes come into play.

推荐答案

根据 ObjectId 文档,您看到的字符串是 12 个字节的十六进制表示.它不是 Unicode,甚至不是字符串.它实际上是一个数字.

As per the ObjectId documentation, that string you see is a hex representation of the 12 bytes. It's not Unicode or even a string. It's actually a number.

一个字节是 8 位,这意味着它可以有 2^8 == 256 个可能的值(参见 Byte).

A byte is 8 bits, meaning that it can have 2^8 == 256 possible values (see Byte).

如何简洁地表示具有 256 个可能值的数字?把它表示为 16^2 怎么样?您可以通过使用 2 个十六进制值(基数为 16)来实现这一点.您唯一需要发明的是一个编号系统,该系统编号为 16 而不是 10.

How do you represent a number with 256 possible values succinctly? How about representing it as 16^2 instead? You can achieve this by using 2 hexadecimal values (base 16). The only thing you need to invent is a numbering system that goes to 16 instead of 10.

事实上,我们使用从af的字母来表示10到15的值.

As a matter of fact, we use letters from a to f to represent values 10 to 15.

因此,一个字节可以用两个十六进制数表示.它只是碰巧使用 af ,因为我们不会费心为它们发明特殊符号.它们不是字母.它们是数字.

Thus, one byte can be represented in two hexadecimal numbers. It just happens to use a to f since we couldn't be bothered to invent special symbols for them. They are not letters. They are numbers.

所以不,你在 ObjectId 中看到的字符串不代表 24 个字节.每 2 个字符代表一个字节.24 个十六进制数 == 12 个字节.

So no, the string you see in ObjectId does not represent 24 bytes. Every 2 characters represent a byte instead. 24 hex numbers == 12 bytes.

这篇关于MongoDB ObjectID 12 字节如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆