OPTIONS

Create a Hashed Index

New in version 2.4.

Hashed indexes compute a hash of the value of a field in a collection and index the hashed value. These indexes permit equality queries and may be suitable shard keys for some collections.

Tip

MongoDB automatically computes the hashes when resolving queries using hashed indexes. Applications do not need to compute hashes.

See

Hashed Shard Keys for more information about hashed indexes in sharded clusters, as well as Index Concepts and Indexing Tutorials for more information about indexes.

Procedure

To create a hashed index, specify hashed as the value of the index key, as in the following example:

Example

Specify a hashed index on _id

db.collection.ensureIndex( { _id: "hashed" } )

Considerations

MongoDB supports hashed indexes of any single field. The hashing function collapses sub-documents and computes the hash for the entire value, but does not support multi-key (i.e. arrays) indexes.

You may not create compound indexes that have hashed index fields.