mongodb _id
MongoDB is a NoSQL database that operates with collections and documents. Each document created on MongoDB has a unique object ID property. So when creating a document without entering an ID, the document will be created with an auto-generated ID.
MongoDB是一个NoSQL数据库,用于处理集合和文档。 在MongoDB上创建的每个文档都有一个唯一的对象ID属性。 因此,在不输入ID的情况下创建文档时,将使用自动生成的ID创建文档。
谁生成ID? (Who Generates the ID?)
When filling in the properties of a document, we do not necessarily need to enter the object ID. But when we refer to MongoDB after creating a document, it would have an object ID that looks like this:
填写文档的属性时,我们不一定需要输入对象ID。 但是,当我们在创建文档后引用MongoDB时,它将具有一个如下所示的对象ID:
{
“_id”: “5f1819229fdf8a0c7c2d8c36”
}
This makes it much easier for us when creating documents in MongoDB and saves us a lot of time. The object ID in the documents in MongoDB is created by the MongoDB driver, which talks to MongoDB. Therefore, this brings out a lot of advantages:
这使我们在MongoDB中创建文档时更加容易,并节省了很多时间。 MongoDB中文档中的对象ID由与MongoDB对话的MongoDB驱动程序创建。 因此,这带来了很多优点:
- You do not need to wait for MongoDB to create a new unique identifier. 您无需等待MongoDB创建新的唯一标识符。
- Applications of MongoDB are highly scalable. MongoDB的应用程序具有高度可扩展性。
- You can create several instances of MongoDB. 您可以创建多个MongoDB实例。
- There is no need to talk to a central place to get a unique identifier. 无需与中心位置交谈即可获取唯一的标识符。
对象ID的属性 (Properties of the Object ID)
An object ID is 24 characters long with two characters taking up to one byte, thus containing a total of 12 bytes. Here is what the 12 bytes of the object ID tell us.
对象ID为24个字符长,其中两个字符最多占用一个字节,因此总共包含12个字节。 这是对象ID的12个字节告诉我们的内容。
时间戳记 (Timestamp)
The first four bytes of the object ID represent the timestamp when the document was created. We avoid the need to create a separate property in the document such as created-at
and can thus save time and have optimized lines of code.
对象ID的前四个字节代表创建文档时的时间戳。 我们避免了在文档中创建单独的属性(例如created-at
,因此可以节省时间并优化代码行。
Due to the contribution of the timestamp to the object ID, we can obtain the time when the document was created by referring to the ID exclusively. Therefore, when querying the data, we do not need a different method to sort the documents by the timestamp.
由于时间戳对对象ID的贡献,我们可以通过专门引用ID来获取创建文档的时间。 因此,在查询数据时,我们不需要其他方法即可根据时间戳对文档进行排序。
You can obtain the timestamp of an object by using the following commands:
您可以使用以下命令获取对象的时间戳:
const mongoose = require('mongoose');// Create object Id on memory
const id = new mongoose.Types.ObjectId();// Get timestamp
console.log(id.getTimestamp());
机器识别码 (Machine identifier)
The next three bytes represent the machine identifier (i.e. the machine that the document was created on). Suppose two documents were created at the same time on different machines. These three bytes would be different, adding to the uniqueness of the object ID.
接下来的三个字节代表机器标识符(即在其上创建文档的机器)。 假设在不同的计算机上同时创建了两个文档。 这三个字节将有所不同,从而增加了对象ID的唯一性。
进程标识符 (Process identifier)
The next two bytes represent the process identifier (i.e. the process in the machine that the document was created on). Suppose two documents were created at the same time on the same machine, but with different processes. These two bytes will be different, adding to the uniqueness of the object ID.
接下来的两个字节表示进程标识符(即,在其上创建文档的计算机中的进程)。 假设在同一台计算机上同时创建了两个文档,但是流程不同。 这两个字节将有所不同,从而增加了对象ID的唯一性。
计数器 (Counter)
The last three bytes represent a counter. This counter is an auto-incrementing number similar to other counter variables in SQL and NoSQL databases that makes the object ID unique (in SQL, this may hinder scalability). Suppose two documents were created at the same time on the same machine and on the same process. The counter bytes will be different, contributing to the uniqueness of the object ID.
最后三个字节代表一个计数器。 该计数器是一个自动递增的数字,类似于SQL和NoSQL数据库中的其他计数器变量,该变量使对象ID唯一(在SQL中,这可能会妨碍可伸缩性)。 假设同时在同一台计算机和同一进程上创建了两个文档。 计数器字节将有所不同,从而有助于对象ID的唯一性。
对象ID问题 (The Object ID Problem)
In MongoDB, a problem arises with the counter in the object ID that may limit its uniqueness.
在MongoDB中,对象ID中的计数器出现问题,可能会限制其唯一性。
The object ID is only unique as long as the counter does not overflow!
只要计数器不溢出,对象ID就是唯一的!
The counter overflow problem is when the counter has reached its maximum capacity, leading to documents having the same object ID. Therefore, the object ID is almost unique but not 100% unique!
计数器溢出问题是当计数器达到其最大容量时,导致文档具有相同的对象ID。 因此,对象ID几乎是唯一的,但不是100%唯一!
Here is why: The counter is being allocated three bytes. This means it has the capacity to represent up to 16 million numbers. If that many documents are generated at the same time, on the same machine, and the same process, two documents can share the same object ID.
原因如下:计数器被分配了三个字节。 这意味着它最多可以代表1600万 数字。 如果在同一台计算机,同一进程上同时生成了许多文档,则两个文档可以共享相同的对象ID。
使用猫鼬 (Using Mongoose)
When building applications using Node.js and Express.js, we use mongoose. Mongoose is an abstraction over the MongoDB driver. Therefore, when creating a document, mongoose talks to the MongoDB driver to create a new object ID.
使用Node.js和Express.js构建应用程序时,我们使用mongoose 。 Mongoose是MongoDB驱动程序的抽象。 因此,在创建文档时,猫鼬会与MongoDB驱动程序对话以创建新的对象ID。
创建一个对象ID (Creating an object ID)
// create an object id on memory - Not on DB!const mongoose = require('mongoose');const id = mongoose.Types.ObjectId();
console.log(id);// output -> 5f1819229fdf8a0c7c2d8c36
验证对象ID (Validating object ID)
With mongoose, you can also validate the object ID statically using the isValid
property:
使用mongoose,您还可以使用isValid
属性静态验证对象ID:
结论 (Conclusion)
The MongoDB object ID is unique unless a certain scenario (discussed above) is reached. Therefore, as software developers, we should consider the size and complexity of the system for a trade-off between the auto-generated ID and a custom object ID.
除非达到特定情况(如上文所述),否则MongoDB对象ID是唯一的。 因此,作为软件开发人员,我们应该考虑系统的大小和复杂性 ,以在自动生成的ID和自定义对象ID之间进行权衡。
The approach is entirely up to you to decide. For more information regarding the MongoDB Object ID, you can refer to the official documentation of MongoDB and Mongoose.
该方法完全由您决定。 有关MongoDB对象ID的更多信息,您可以参考MongoDB和Mongoose的官方文档。
I hope this story has educated you on the uniqueness and reliability of the Object ID in MongoDB. Have fun learning and enjoy coding!
我希望这个故事能使您了解MongoDB中对象ID的独特性和可靠性。 祝您学习愉快并享受编码!
翻译自: https://medium.com/better-programming/is-the-id-property-in-mongodb-100-unique-f1eaa19522ba
mongodb _id