Mongodb-分片时_id必须是全局唯一的 [英] Mongodb - must _id be globally unique when sharding

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

问题描述

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

警告在任何分片集合中,如果您不通过分片 _id字段,您必须确保_id字段的唯一性.确保_id始终唯一的最佳方法是使用ObjectId或其他方法 通用唯一标识符(UUID.)

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

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

解决方案

除非您手动替换它们,否则自动生成的_id是UUID,根据

如您所见,唯一的机器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.

Warning In any sharded collection where you are not sharding by the _id field, you must ensure uniqueness of the _id field. The best way to ensure _id is always unique is to use ObjectId, or another universally unique identifier (UUID.)

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必须是全局唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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