Hashed Indexes

Hashed indexes maintain entries with hashes of the values of theindexed field.

Hashed indexes support shardingusing hashed shard keys. Hashed based sharding uses a hashed index of a field as the shardkey to partition data across your sharded cluster.

Using a hashed shard key to shard a collection results in a more randomdistribution of data. SeeDeploy Sharded Cluster using Hashed Sharding for moredetails.

Hashing Function

Hashed indexes use a hashing function to compute the hash of the valueof the index field. [1] The hashing function collapsesembedded documents and computes the hash for the entire value but doesnot support multi-key (i.e. arrays) indexes.

Tip

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

[1]Starting in version 4.0, the mongo shell provides themethod convertShardKeyToHashed(). This method uses thesame hashing function as the hashed index and can be used to seewhat the hashed value would be for a key.

Create a Hashed Index

To create a hashed index, specifyhashed as the value of the index key, as in the followingexample:

  1. db.collection.createIndex( { _id: "hashed" } )

Considerations

MongoDB supports hashed indexes of any single field. The hashingfunction collapses embedded documents and computes the hash for the entirevalue, but does not support multi-key (i.e. arrays) indexes.

You may not create compound indexes that have hashed index fieldsor specify a unique constraint on a hashed index; however, you cancreate both a hashed index and an ascending/descending (i.e.non-hashed) index on the same field: MongoDB will use the scalar indexfor range queries.

253 Limit

Warning

MongoDB hashed indexes truncate floating point numbers to 64-bit integersbefore hashing. For example, a hashed index would store the samevalue 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 floatingpoint numbers that cannot be reliably converted to 64-bitintegers (and then back to floating point). MongoDB hashed indexes donot support floating point values larger than 253.

To see what the hashed value would be for a key, seeconvertShardKeyToHashed().

PowerPC and 263

For hashed indexes, MongoDB 4.2 ensuresthat the hashed value for the floating point value 263 onPowerPC is consistent with other platforms.

Although hashed indexes on a field that maycontain floating point values greater than 253 is anunsupported configuration, clients may still insert documents where theindexed field has the value 263.

To list all hashed indexes for allcollections in your deployment, you can use the followingoperation in the mongo shell:

  1. db.adminCommand("listDatabases").databases.forEach(function(d){
  2. let mdb = db.getSiblingDB(d.name);
  3. mdb.getCollectionInfos({ type: "collection" }).forEach(function(c){
  4. let currentCollection = mdb.getCollection(c.name);
  5. currentCollection.getIndexes().forEach(function(idx){
  6. let idxValues = Object.values(Object.assign({}, idx.key));
  7.  
  8. if (idxValues.includes("hashed")) {
  9. print("Hashed index: " + idx.name + " on " + idx.ns);
  10. printjson(idx);
  11. };
  12. });
  13. });
  14. });

To check if the indexed field contains the value 263, run thefollowing operation for the collection and the indexed field:

  • If the indexed field type is a scalar and never a document:
  1. // substitute the actual collection name for <collection>
  2. // substitute the actual indexed field name for <indexfield>
  3.  
  4. db.<collection>.find( { <indexfield>: Math.pow(2,63) } );
  • If the indexed field type is a document (or a scalar), you can run:
  1. // substitute the actual collection name for <collection>
  2. // substitute the actual indexed field name for <indexfield>
  3.  
  4. db.<collection>.find({
  5. $where: function() {
  6. function findVal(obj, val) {
  7. if (obj === val)
  8. return true;
  9.  
  10. for (const child in obj) {
  11. if (findVal(obj[child], val)) {
  12. return true;
  13. }
  14. }
  15. return false;
  16. }
  17. return findVal(this.<indexfield>, Math.pow(2, 63));
  18. }
  19. })