Docs Menu
Docs Home
/
MongoDB Manual
/ / /

convertShardKeyToHashed

On this page

  • Description
  • Compatibility
  • Example
convertShardKeyToHashed(<Object>)

Returns the hashed value for the input. The convertShardKeyToHashed() method uses the same hashing function as the hashed index and can be used to see what the hashed value would be for a key.

This method is available in deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

Note

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

Consider a sharded collection that uses a hashed shard key [1]

use test
db.orders.createIndex( { _id: "hashed" } )
sh.shardCollection( "test.orders", { _id : "hashed" } )

If the following document exists in the collection, the hashed value of the _id field is used to distribute the document:

{
_id: ObjectId("5b2be413c06d924ab26ff9ca"),
"item" : "Chocolates",
"qty" : 25
}

To determine the hashed value of _id field used to distribute the document across the shards, you can use the convertShardKeyToHashed() method:

convertShardKeyToHashed( ObjectId("5b2be413c06d924ab26ff9ca") )
[1] If the collection already contains data, you must create a hashed index on the shard key before you shard the collection. For an empty collection, MongoDB creates the index as part of sh.shardCollection().

Back

Sharding