当 MongoDB 的文档说 ObjectIDs“可能是唯一的"时,它是什么意思? [英] What does MongoDB's documentation mean when it says ObjectIDs are "likely unique"?

查看:69
本文介绍了当 MongoDB 的文档说 ObjectIDs“可能是唯一的"时,它是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:ObjectIds 的 MongoDB 文档 表示ObjectIds 很小,可能是唯一的,生成速度快,而且有序."

Situation: The MongoDB documentation for ObjectIds says "ObjectIds are small, likely unique, fast to generate, and ordered."

问题:为什么 MongoDB 的 ObjectId 只是可能唯一,而不是完全唯一?

Question: Why and how would MongoDB's ObjectIds be merely likely unique, rather than fully unique?

我的尝试: 谷歌搜索并粗略搜索 SO.其他一些 SO 问题似乎指的是非唯一性问题,但我还没有看到一个完整的解释.也有一些 Medium 文章可能解决了这个问题,但在前几篇文章之后,Medium 不是免费的.

What I've tried: Googling and doing a cursory search on SO. Some other SO questions seem to refer to the problem of non-uniqueness, but I haven't seen one with a full explanation yet. There are also some Medium articles that may address the point, but Medium is not free after the first few articles.

推荐答案

问题:为什么 MongoDB 的 ObjectIds 只是可能是唯一的,而不是完全唯一的?

Question: Why and how would MongoDB's ObjectIds be merely likely unique, rather than fully unique?

两个原因:

  1. 每个 ObjectId 都是在客户端本地生成的.不同的客户并没有协调他们产生的价值.因此,可能有两个客户端碰巧生成相同的随机数,从而生成完全相同的 ObjectId.极不可能但可能,因此可能是唯一的"措辞.

  1. Each ObjectId is generated locally on the client. The different clients are not coordinating the values they are generating. Therefore it is possible that there are two clients that happen to generate the same random numbers and would thus generate exactly the same ObjectIds. Highly unlikely but possible, hence "likely unique" wording.

ObjectId 生成算法指定使用用于消除同一秒内生成的值的 3 字节计数器.如果您在同一秒内生成超过 1600 万个 ObjectId,该计数器将溢出,您正在使用的客户端将开始生成它刚才已经生成的 ObjectId.

ObjectId generation algorithm specifies the use of a 3-byte counter that is used to disambiguate the values generated in the same second. If you generate more than 16 million ObjectIds in the same second, that counter will overflow and the client you are using will start to generate ObjectIds it already generated a moment ago.

这篇关于当 MongoDB 的文档说 ObjectIDs“可能是唯一的"时,它是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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