简体   繁体   English

UUID VS MongoDB ObjectId

[英]UUID VS MongoDB ObjectId

I'm developing an application and I'm having some doubts about using MongoDB ObjectId instead UUID as Document ID.我正在开发一个应用程序,我对使用 MongoDB ObjectId 而不是 UUID 作为文档 ID 有一些疑问。

I've read MongoDB documentation (Mongo v4.0 - Mongo v5.0) and it says that ObjectId creation consists of:我已阅读 MongoDB 文档(Mongo v4.0 - Mongo v5.0),它说 ObjectId 创建包括:

  • a 4-byte timestamp value, representing the ObjectId's creation, measured in seconds since the Unix epoch一个 4 字节的时间戳值,表示 ObjectId 的创建,以 Unix 纪元以来的秒数为单位
  • a 5-byte random value generated once per process.每个进程生成一次的 5 字节随机值。 This random value is unique to the machine and process.这个随机值对于机器和过程是唯一的。
  • a 3-byte incrementing counter, initialized to a random value一个 3 字节递增计数器,初始化为随机值

MongoDB ObjectId Documentation MongoDB ObjectId 文档

So currently there is no problem using it because it doesn't specify a 3-byte machine id and a 2-byte process id as previously versions <4.0 and I undersand that is secure.所以目前使用它没有问题,因为它没有指定一个 3 字节的机器 ID 和一个 2 字节的进程 ID,因为以前的版本 <4.0 我知道这是安全的。

Following good practices, if I have the following Document, which option is better?遵循良好实践,如果我有以下文档,哪个选项更好?

A: A:

{
  _id: ObjectId("5c593e5506b79bac64a5873e") //ObjectId
  id: "5c593e5506b79bac64a5873e" //String 
  ...
  subDocuments: [{
    _id: ObjectId("5c593e5506b79bac64a5873e") //ObjectId
    id: "5c593e5506b79bac64a5873e" //String 
  },{
   ...
  }]
}

This object contains the _id as string in "id" parameter to use it in frontend这个 object 在“id”参数中包含 _id 作为字符串,以便在前端使用它

B:乙:

{
  _id: ObjectId("5c593e5506b79bac64a5873e") //ObjectId
  ...
  subDocuments: [{
    _id: ObjectId("5c593e5506b79bac64a5873e") //ObjectId
  },{
   ...
  }]
}

This document has no "id" parameter.该文档没有“id”参数。 It transforms _id ObjectId to string before send the data to frontend.在将数据发送到前端之前,它将 _id ObjectId 转换为字符串。

C: C:

{
  _id: ObjectId("5c593e5506b79bac64a5873e") //ObjectId
  id: UUID
  ...
  subDocuments: [{
    _id: ObjectId("5c593e5506b79bac64a5873e") //ObjectId
    id: UUID
  },{
   ...
  }]
}

This document uses UUID as "id" and ObjectId as "_id".本文档使用 UUID 作为“id”,使用 ObjectId 作为“_id”。

My current CONCLUSION:我目前的结论:

I Should use ObjectId always less when I want to hide Doucment data creation because Mongo ObjectId specifies that timestamp.当我想隐藏文档数据创建时,我应该总是少用 ObjectId,因为 Mongo ObjectId 指定了那个时间戳。

If there is some value that is unique in the collection, you can use it as the _id and not use ObjectId at all.如果集合中有一些唯一的值,您可以将其用作_id而根本不使用 ObjectId。 You lose the functionality provided by ObjectId to get the creation date of the document but, of course, you can use another field for that.您失去了 ObjectId 提供的获取文档创建日期的功能,但当然,您可以使用另一个字段。

In your case, you don't need to generate a UUID to use it as the ID, the ObjectId is creating that unique key for you, so you can send the string with the ObjectId to the frontend and let Mongo create the ObjectIds for you in the database level.在您的情况下,您无需生成 UUID 即可将其用作 ID,ObjectId 正在为您创建唯一键,因此您可以将带有 ObjectId 的字符串发送到前端并让 Mongo 为您创建 ObjectId在数据库级别。

It probably doesn't make to much sense to use both the ObjectId and a UUID field.同时使用 ObjectId 和 UUID 字段可能没有多大意义。 Mongo creates a default index in the _id field and you probably need another one for the ìd field as well to be able to query by id efficiently, so the result is the collection has an extra field and an extra index that is really not needed. Mongo 在_id字段中创建了一个默认索引,您可能还需要另一个用于ìd字段的索引,以便能够有效地按id进行查询,因此结果是集合有一个额外的字段和一个实际上不需要的额外索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM