Docs Home → Develop Applications → MongoDB Manual
Hashed Indexes哈希索引
On this page本页内容
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用例
Using a hashed shard key to shard a collection results in a more even distribution of data between shards compared to ranged sharding.与范围分片相比,使用哈希分片键对集合进行分片可以使数据在分片之间的分布更加均匀。
If your shard key monotonically increases, using a hashed index as the shard key evenly distributes data between shards, as opposed to writing data to the shard with an upper bound of maxKey.如果分片键单调增加,则使用哈希索引作为分片键在分片之间均匀分布数据,而不是将数据写入上限为maxKey的分片。
Common examples of monotonically increasing shard keys are timestamps and ObjectId values.单调增加分片键的常见示例是时间戳和ObjectId值。
Behavior行为
Floating-Point Numbers浮点数字
Hashed indexes truncate floating-point numbers to 64-bit integers before hashing. 散列索引在散列之前将浮点数截断为64位整数。For example, a hashed index uses the same hash to store the values 例如,散列索引使用相同的散列来存储值2.3
, 2.2
, and 2.9
. 2.3
、2.2
和2.9
。This is a collison, where multiple values are assigned to a single hash key. 这是一个碰撞,其中多个值被分配给一个散列键。Collisions may negatively impact query performance.冲突可能会对查询性能产生负面影响。
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 multi-key indexes. 哈希函数不支持多键索引。You cannot create a hashed index on a field that contains an array or insert an array into a hashed indexed field.不能对包含数组的字段创建哈希索引,也不能将数组插入哈希索引字段。
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哈希函数
When MongoDB uses a hashed index to resolve a query, it uses a hashing function to automatically compute the hash values. 当MongoDB使用哈希索引来解析查询时,它会使用哈希函数来自动计算哈希值。Applications do not need to compute hashes.应用程序不需要计算散列。
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.散列函数折叠嵌入的文档并计算整个值的散列。