Docs Home → Develop Applications → MongoDB Manual
Create a Hashed Index
On this page
To enable sharding for a collection that already contains data, you must create an index that supports the shard key. To enable sharding for an empty collection, you can instead specify the shard key index when you shard the collection.
Hashed indexes support hashed sharding. A hashed index acts as a shard key to distribute data across shards based on hashes of field values.
To create a single-field hashed index, specify hashed
as the value
of the index key:
db.<collection>.createIndex( { <field>: "hashed" } )
To create a hashed index that contains multiple fields (a compound
hashed index), specify hashed
as the value of a single index key.
For other index keys, specify the sort order (1
or -1
):
db.<collection>.createIndex( { <field1>: "hashed", <field2>: "<sort order>", <field3>: "<sort order>", ... } )
About this Task
Your hashed index can contain either a single field or multiple fields. The fields in your index specify how data is distributed across shards in your cluster.
Consider the following guidelines for your hashed shard key:
The field you choose for your hashed shard key should have a high cardinality, meaning a large number of different values. Hashed indexing is ideal for shard keys with fields that change monotonically like ObjectId values or timestamps.
If your data model does not contain a single field with high cardinality, consider creating a compound hashed index. A compound hashed index provides more unique indexed values and can increase cardinality.
A hashed index can contain up to 32 fields.
Before You Begin
To implement hashed sharding, you must deploy a sharded cluster.
Examples
The following examples show you how to:
Create a Single-Field Hashed Index
Consider an orders
collection that already contains data. Create a
hashed index in the orders
collection on the _id
field:
db.orders.createIndex( { _id: "hashed" } )
The _id
field increases monotonically, which makes it a good
candidate for a hashed index key. Although _id
values incrementally
increase, when MongoDB generates a hash for individual _id
values,
those hashed values are unlikely to be on the same chunk.
After you create the index, you can shard the orders
collection:
sh.shardCollection( "<database>.orders", { _id: "hashed" } )
Create a Compound Hashed Index
Consider a customers
collection that already contains data. Create a
compound hashed index in the customers
collection on the name
,
address
, and birthday
fields:
db.customers.createIndex( { "name" : 1 "address" : "hashed", "birthday" : -1 } )
When you create a compound hashed index, you must specify hashed
as
the value of a single index key. For other index keys, specify the
sort order (1
or -1
). In the preceding index, address
is the
hashed field.
After you create the index, you can shard the customers
collection:
sh.shardCollection( "<database>.customers", { "name" : 1 "address" : "hashed", "birthday" : -1 } )