- Indexes >
- Index Concepts >
- Index Types >
- Hashed Index
Hashed Index¶
New in version 2.4.
Hashed indexes maintain entries with hashes of the values of the indexed field. The hashing function collapses embedded documents and computes the hash for the entire value but does not support multi-key (i.e. arrays) indexes.
Hashed indexes support sharding a collection using a hashed shard key. Using a hashed shard key to shard a collection ensures a more even distribution of data. See Shard a Collection Using a Hashed Shard Key for more details.
MongoDB can use the hashed
index to support equality queries, but
hashed
indexes do not support range queries.
You may not create compound indexes that have hashed
index fields
or specify a unique constraint on a hashed
index; however, you can
create both a hashed
index and an ascending/descending (i.e.
non-hashed) index on the same field: MongoDB will use the scalar index
for range queries.
Warning
MongoDB hashed
indexes truncate floating point numbers to 64-bit integers
before hashing. 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
.
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). MongoDB hashed
indexes do
not support floating point values larger than 253.
Create a hashed
index using an operation that resembles the
following:
This operation creates a hashed index for the active
collection on
the a
field.