本文介绍了Mongodb-分片时_id必须是全局唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过其外键"(用户ID)而不是其ID字段来分片集合.我只需要userID和id的组合是唯一的.但是我不确定mongodb是否可以.

它取自: http://docs.mongodb.org/manual/tutorial/enforce-unique-keys-for-sharded-collections/#enforce-unique-keys-for-sharded-collections

我必须确保_id是唯一的吗?还是我总是同时使用userID和_id进行查询就足够了吗?

解决方案

除非您手动替换它们,否则自动生成的_id是UUID,根据文档,包括"4字节时间戳(自纪元以来的秒数),3字节机器ID,2字节字节进程ID和3字节计数器".

如您所见,唯一的机器ID是UUID的一部分.这样可以确保分片中没有两台机器独立创建相同的UUID(除非它们具有相同的机器ID-相似的可能性是1:16777215,当它发生时可以很容易地进行验证).从理论上讲,唯一可以复制UUID的情况是,单个进程在一秒钟内创建了2 ^ 24个(超过1600万个)UUID.

tl; dr:您不必担心重复的UUID-正如文档所述,它们是被设计为在分配时具有相当高的唯一性". /p>

I want to shard a collection by their "foreign key" (userID) and not by their id field. I only need that the combination of userID and id is unique. But I am not sure if that is ok with mongodb.

This is taken from: http://docs.mongodb.org/manual/tutorial/enforce-unique-keys-for-sharded-collections/#enforce-unique-keys-for-sharded-collections

Do I have to ensure that _id is unique? Or is it good enough if I always query by both userID and _id?

解决方案

Unless you manually replace them, the auto-generated _id's are UUID's which, according to the documentation, consist of "a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter".

As you can see, an unique machine-ID is part of the UUID. That ensures that no two machines in the shard ever create the same UUID independently (unless they have the same machine-id - the likeliness for that is 1:16777215 and when it happens it can be easily verified). The only situation where you could theoretically have a duplicated UUID is when a single process creates more than 2^24 (over 16 million) UUIDs in a single second.

tl;dr: You don't have to worry about duplicate UUIDs - they are, as the documentation puts it, "designed to have a reasonably high probability of being unique when allocated".

这篇关于Mongodb-分片时_id必须是全局唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:57