The Case for MongoDB® Hashed Indexes

2 min read
The Case for MongoDB® Hashed Indexes

SHARE THIS ARTICLE

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:

db.collection.ensureIndex({'field':'hashed'});

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

Pros

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
}

Cons

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 www.scalegrid.io. 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

blog-feature-img_whats-new-at-scalegrid

What’s New at ScaleGrid – September 2024

At ScaleGrid, we’re always pushing the boundaries to offer more flexibility and scalability to our customers. Over the past few...

Managing PostgreSQL® High Availability – Part I: PostgreSQL Automatic Failover

Managing High Availability (HA) in your PostgreSQL hosting is very important to ensuring your database deployment clusters maintain exceptional uptime...

RabbitMQ Security Compliance - ScaleGrid

RabbitMQ Security and Compliance

Follow fundamental procedures in authentication, encryption, and commitment to RabbitMQ security protocols to protect your RabbitMQ system and secure messages....

NEWS

Add Headline Here