Hashed indexes collect and store hashes of the values of the indexed field.哈希索引集合并存储索引字段值的哈希值。
Hashed indexes support sharding using hashed shard keys. Hashed based sharding uses a hashed index of a field as the shard key to partition data across your sharded cluster.哈希索引支持使用哈希分片键进行分片。基于哈希的分片使用字段的哈希索引作为分片键,在分片集群中对数据进行分区。
Use Cases用例
Hashed indexing is ideal for shard keys with fields that change monotonically like ObjectId values or timestamps. 哈希索引非常适合具有单调变化的字段(如ObjectId值或时间戳)的分片键。When you use ranged sharding with a monotonically increasing shard key value, the chunk with an upper bound of 当您使用具有单调递增的分片键值的范围分片时,上限为MaxKey receives the majority incoming writes. This behavior restricts insert operations to a single shard, which removes the advantage of distributed writes in a sharded cluster.MaxKey的块会接收大多数传入写入。这种行为将插入操作限制在单个分片上,从而消除了分片集群中分布式写入的优势。
For more information on choosing the best sharding approach for your application, see Hashed vs Ranged Sharding有关为应用程序选择最佳分片方法的更多信息,请参阅哈希分片与范围分片的对比
Behavior行为
Floating-Point Numbers浮点数字
Hashed indexes truncate floating-point numbers to 64-bit integers before hashing. For example, a hashed index uses the same hash to store the values 哈希索引在哈希之前将浮点数截断为64位整数。例如,哈希索引使用相同的哈希来存储值2.3, 2.2, and 2.9. This is a collision, where multiple values are assigned to a single hash key. Collisions may negatively impact query performance.2.3、2.2和2.9。这是一个冲突,其中多个值被分配给一个哈希键。冲突可能会对查询性能产生负面影响。
To prevent collisions, do not use a hashed index for floating-point numbers that cannot be reliably converted to 64-bit integers and then back to floating point.为了防止冲突,不要对不能可靠地转换为64位整数然后再转换回浮点的浮点数使用哈希索引。
Hashed indexes do not support floating-point numbers larger than 2 53.哈希索引不支持大于253的浮点数。
Limitations局限性
Hashed indexes have limitations for array fields and the unique property.哈希索引对数组字段和唯一属性有限制。
Array Fields数组字段
The hashing function does not support multikey indexes:哈希函数不支持多键索引:
You cannot create a hashed index on a field that contains an array or insert an array into a hashed indexed field.您不能在包含数组的字段上创建哈希索引,也不能将数组插入到哈希索引字段中。If any field in a compound index is an array, no field in that index can use a hashed index. This includes non-array fields.如果复合索引中的任何字段都是数组,则该索引中的字段都不能使用哈希索引。这包括非数组字段。You cannot use hashed indexes in a compound index that becomes a multikey index.不能在成为多键索引的复合索引中使用哈希索引。
Covered Queries涵盖的查询
Hashed indexes can't cover a query.哈希索引不能覆盖查询。
Unique Constraint唯一约束
You cannot specify a unique constraint on a hashed index. Instead, you can create an additional non-hashed index with the unique constraint. MongoDB can use that non-hashed index to enforce uniqueness on the chosen field.不能对哈希索引指定唯一约束。相反,您可以使用唯一约束创建一个额外的非哈希索引。MongoDB可以使用该非哈希索引来强制所选字段的唯一性。
Get Started开始使用
To create a hashed index, see Create a Hashed Index.要创建哈希索引,请参阅创建哈希索引。
Details详情
This section describes technical details for hashed indexes.本节介绍哈希索引的技术细节。
Hashing Function哈希函数
Important
When MongoDB uses a hashed index to resolve a query, it uses a hashing function to automatically compute the hash values. Applications do not need to compute hashes.当MongoDB使用哈希索引解析查询时,它使用哈希函数自动计算哈希值。应用程序不需要计算哈希值。
To see what the hashed value would be for a key, use the 要查看键的哈希值,请使用convertShardKeyToHashed() method. convertShardKeyToHashed()方法。This method uses the same hashing function as the hashed index.此方法使用与哈希索引相同的哈希函数。
Embedded Documents嵌入式文档
The hashing function collapses embedded documents and computes the hash for the entire value.哈希函数折叠嵌入的文档,并计算整个值的哈希值。