Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-11656

Document convertShardKeyToHashed function

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 3.7.6
    • None
    • None

    Description

      Description:

      This change added in a mongo shell function "convertShardKeyToHashed". This function converts a bson key value pair into the hashed value that would be used for indexing/sharding.

      Example usage from the source:

      /**
       * This function computes a hash value for a document.
       * Specifically, this is the same hash function that is used to form a hashed index,
       * and thus used to generate shard keys for a collection.
       *
       * e.g.
       * > // For a given collection prepared like so:
       * > use mydb
       * > db.mycollection.createIndex({ x: "hashed" })
       * > sh.shardCollection("mydb.mycollection", { x: "hashed" })
       * > // And a sample object like so:
       * > var obj = { x: "Whatever key", y: 2, z: 10.0 }
       * > // The hashed value of the shard key can be acquired from the shard key-value pair like so:
       * > convertShardKeyToHashed({x: "Whatever key"})
       */
      

      Scope of changes:

      • 4.0
      • new ref page and toc pages
      • add link to new page from hash index page
      • add link to new page from /core/hashed-sharding.txt

      Not adding to general mentions about hashed sharding/hashed indexes; e.g.:

      • indexes
      • sharding
      • move chunk – since you would sh.status to get the bounds of the chunk to move
      • etc.

      Impact to other docs outside of this product:

      none

      MVP:

      Resources:

      Engineering Ticket Description:

      Users may sometimes want to figure out the hashed value for a certain document's shard key field. For instance the reason for doing that might be to identify the shard key range / chunk the document belongs to (when a hashed shard key is used). There a ways to do that, namely:

      • Run a query on the shard key and inspect the index key (the `$returnKey` query option)
      • Run a query on the shard key and inspect the index scan bounds in the $explain output
      • Re-start the server in test mode and use the _hashBSONElement command
      • Port BSONElementHasher into a custom application

      All of these methods are pretty clumsy and inefficient when all you need is to get a hashed value for an arbitrary field. It would be much user-friendly if BSONElementHasher could be run on the client side (i.e. in the mongo shell) via a helper function.

      Attachments

        Activity

          People

            kay.kim@mongodb.com Kay Kim (Inactive)
            kay.kim@mongodb.com Kay Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              5 years, 33 weeks, 6 days ago