The Case for MongoDB® Hashed Indexes

2 min read
The Case for MongoDB® Hashed Indexes


In addition to scalar indexes (ascending, descending), MongoDB® also supports ‘hashed’ indexes.  When you use a hashed index on a field, MongoDB computes a hash of the field value and stores the hash in the index. Hashed indexes support only equality comparison and do not support range queries, and are typically used in sharding scenarios.

Creating a MongoDB Hashed Index

You can use the following syntax to create a hashed index:


You can create both a scalar index and a hashed index on the same field.

MongoDB Sharding

Typically, MongoDB sharding is implemented using ‘range-based’ partitioning. In this approach, each shard is assigned a range of values of the shard key.  If the shard key is monotonically increasing like timestamps or objectID, this can sometimes result in a ‘hot’ shard because the most recent values always end up being routed to the same shard.

The way to get around this is to use ‘hash-based partitioning’.  A hash of the shard key is computed and this hash value is used to route to a shard instead of the actual value. This helps to distribute load evenly across all the shards instead of sending all the newest data to the same shard. Hash-based partitioning is implemented using hashed indexes on your shard key.  For more information, refer to the sharding documentation.

Hashed Index Pros & Cons


Hashed indexes tend to be smaller than the scalar indexes because only a hash of the key is stored instead of the full key. E.g. In a simple test with 100k documents, we added hashed and scalar indexes on a string field – ‘fieldName’. As shown below, the hashed index tends to be considerably smaller than the scalar indexes:

"indexSizes" : {
    "_id_" : 811008,
    "firstName_1" : 4415488,
    "firstName_hashed" : 1490944


Does not support range queries. If you run a range query on a hashed index it will result in a index scan.

Hashed Index Constraints

    1. Hashed indexes do not support arrays.
    2. Hashed indexes cannot be compound indexes.
    3. You cannot add unique constraints on hashed indexes.
For more information, please visit Connect with ScaleGrid on LinkedIn, X, Facebook, and YouTube.
Table of Contents

Stay Ahead with ScaleGrid Insights

Dive into the world of database management with our monthly newsletter. Get expert tips, in-depth articles, and the latest news, directly to your inbox.

Related Posts

Redis vs Memcached in 2024

Choosing between Redis and Memcached hinges on specific application requirements. In this comparison of Redis vs Memcached, we strip away...

multi cloud plan - scalegrid

Plan Your Multi Cloud Strategy

Thinking about going multi-cloud? A well-planned multi cloud strategy can seriously upgrade your business’s tech game, making you more agile....

hybrid cloud strategy - scalegrid

Mastering Hybrid Cloud Strategy

Mastering Hybrid Cloud Strategy Are you looking to leverage the best private and public cloud worlds to propel your business...


Add Headline Here