MongoDB 是否在使用带有 _id 的复合分片键时确保唯一的 _id 字段值 [英] Does MongoDB ensure unique _id field values when using a compound shard key with _id

查看:31
本文介绍了MongoDB 是否在使用带有 _id 的复合分片键时确保唯一的 _id 字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启动分片.如您所知,片键非常重要.我发现,MongoDB 在使用 _id 以外的分片键时不确保唯一的 _id 字段值.

在我们的集合中,用户名应该是分片键.如果我创建复合分片键并使用 _id 作为分片键的第二部分,mongoDB 是否保证 _id 的唯一性?

解决方案

当用作复合键时,MongoDB 无法确保跨分片的 _id 字段是唯一的.

文档 指出:<块引用>

MongoDB 可以强制分片键的唯一性.对于复合分片键,MongoDB 将强制整个键组合的唯一性,并且不适用于分片键的特定组件.

因此,如果您希望 mongo 强制执行电子邮件的唯一性,那么只需将电子邮件用作分片键即可.

电子邮件地址有一些随机性,这很好(_id 内置了一些可预测性),但我建议您将电子邮件字段用作 散列分片键.

I want to initiate the sharding. as you know shard key is very important. I found, MongoDB doesn't ensure unique _id field values when using a shard key other than _id.

In our collections, username shoud be shard key. If i create compound shard key and use _id as second part of shard key, does mongoDB guarantee uniqueness of _id ?

解决方案

MongoDB does not ensure unique _id fields across shards when used as a compound key.

The documentation states :

MongoDB can enforce uniqueness for the shard key. For compound shard keys, MongoDB will enforce uniqueness on the entire key combination, and not for a specific component of the shard key.

So you if you want mongo to enforce uniqueness of the email, then simply use the email as the shard key.

An email address has some randomness, which is good (_id has some predictability built in), but I suggest you use the email field as a hashed shard key.

这篇关于MongoDB 是否在使用带有 _id 的复合分片键时确保唯一的 _id 字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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