convertShardKeyToHashed

Description

  • convertShardKeyToHashed()
  • Returns the hashed value for the input. TheconvertShardKeyToHashed() method uses the same hashingfunction as the hashed index and can be used to see what thehashed value would be for a key.
  • Example

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

    1. use test
    2.  
    3. db.orders.createIndex( { _id: "hashed" } )
    4.  
    5. sh.shardCollection( "test.orders", { _id : "hashed" } )

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

    1. {
    2. _id: ObjectId("5b2be413c06d924ab26ff9ca"),
    3. "item" : "Chocolates",
    4. "qty" : 25
    5. }

    To determine the hashed value of _id field used to distribute thedocument across the shards, you can use theconvertShardKeyToHashed method:

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