On this page本页内容
Hashed indexes maintain entries with 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.基于散列的分片使用字段的散列索引作为分片键,在分片集群中对数据进行分区。
Using a hashed shard key to shard a collection results in a more random distribution of data. 使用散列切分键对集合进行切分会导致数据分布更加随机。See Hashed Sharding for more details.有关更多详细信息,请参阅散列切分。
Hashed indexes use a hashing function to compute the hash of the value of the index field. 散列索引使用散列函数计算索引字段值的散列。[1] The hashing function collapses embedded documents and computes the hash for the entire value but does not support multi-key (i.e. arrays) indexes. 哈希函数折叠嵌入的文档并计算整个值的哈希,但不支持多键(即数组)索引。Specifically, creating a hashed index on a field that contains an array or attempting to insert an array into a hashed indexed field returns an error.具体来说,在包含数组的字段上创建哈希索引或尝试将数组插入哈希索引字段会返回错误。
Tip
MongoDB automatically computes the hashes when resolving queries using hashed indexes. 当使用散列索引解析查询时,MongoDB会自动计算散列。Applications do not need to compute hashes.应用程序不需要计算散列。
| [1] | mongo shell provides the method convertShardKeyToHashed(). mongo shell提供了convertShardKeyToHashed()方法。 |
To create a hashed index, specify 要创建哈希索引,请指定hashed as the value of the index key, as in the following example:hashed作为索引键的值,如下例所示:
New in version 4.4.版本4.4中的新功能。
Starting with MongoDB 4.4, MongoDB supports creating compound indexes that include a single hashed field. 从MongoDB 4.4开始,MongoDB支持创建包含单个散列字段的复合索引。To create a compound hashed index, specify 要创建复合哈希索引,请在创建索引时将哈希指定为任何单个索引键的值:hashed as the value of any single index key when creating the index:
Compound hashed indexes require featureCompatibilityVersion set to 复合哈希索引要求featureCompatibilityVersion设置为4.4.4.4。
See also参阅
The hashing function collapses embedded documents and computes the hash for the entire value, but does not support multi-key (i.e. arrays) indexes. 哈希函数折叠嵌入的文档并计算整个值的哈希,但不支持多键(即数组)索引。Specifically, creating a hashed index on a field that contains an array or attempting to insert an array to a hashed indexed field returns an error.具体来说,在包含数组的字段上创建散列索引或尝试将数组插入散列索引字段会返回错误。
MongoDB does not support specifying a unique constraint on a MongoDB不支持在hashed index. hashed索引上指定唯一约束。You can instead create an additional non-hashed index with the unique constraint on that field. 您可以在该字段上创建一个具有唯一约束的附加非散列索引。MongoDB can use that non-hashed index for enforcing uniqueness on the field.MongoDB可以使用该非散列索引来强制字段的唯一性。
Warning警告
MongoDB MongoDBhashed indexes truncate floating point numbers to 64-bit integers before hashing. hashed索引在散列之前将浮点数截断为64位整数。For example, a 例如,hashed index would store the same value for a field that held a value of 2.3, 2.2, and 2.9. hashed索引将为一个值为2.3、2.2和2.9的字段存储相同的值。To prevent collisions, do not use a 为了防止冲突,不要对无法可靠转换为64位整数(然后再转换回浮点)的浮点数使用hashed index for floating point numbers that cannot be reliably converted to 64-bit integers (and then back to floating point). hashed索引。MongoDB MongoDBhashed indexes do not support floating point values larger than 253.hashed索引不支持大于253的浮点值。
To see what the hashed value would be for a key, see 要查看键的哈希值,请参阅convertShardKeyToHashed().convertShardKeyToHashed()。
For hashed indexes, MongoDB 4.2 ensures that the hashed value for the floating point value 263 on PowerPC is consistent with other platforms.对于散列索引,MongoDB 4.2确保PowerPC上浮点值263的散列值与其他平台一致。
Although hashed indexes on a field that may contain floating point values greater than 253 is an unsupported configuration, clients may still insert documents where the indexed field has the value 263.虽然对可能包含大于253的浮点值的字段进行哈希索引是不受支持的配置,但客户端仍可以在索引字段的值为263的地方插入文档。
To list all 要列出部署中所有集合的所有hashed indexes for all collections in your deployment, you can use the following operation in the mongo shell:hashed索引,可以在mongo shell中使用以下操作:
To check if the indexed field contains the value 263, run the following operation for the collection and the indexed field:要检查索引字段是否包含值263,请对集合和索引字段运行以下操作: