On this page
Hashed indexes collect and store hashes of the values of the indexed field.
Using a hashed shard key to shard a collection results in a more even distribution of data between shards compared to ranged sharding.
Common examples of monotonically increasing shard keys are timestamps and ObjectId values.
Hashed indexes truncate floating-point numbers to 64-bit integers before
hashing. For example, a hashed index uses the same hash to store the
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.
Hashed indexes do not support floating-point numbers larger than 2 53.
Hashed indexes have limitations for array fields and the unique property.
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.
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.
To create a hashed index, see Create a Hashed Index.
This section describes technical details for hashed indexes.
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.
To see what the hashed value would be for a key, use the
convertShardKeyToHashed() method. This method uses the same
hashing function as the hashed index.
The hashing function collapses embedded documents and computes the hash for the entire value.