[DOCS-11656] Document convertShardKeyToHashed function Created: 25/Apr/18  Updated: 29/Oct/23  Resolved: 21/Jun/18

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 3.7.6

Type: Task Priority: Major - P3
Reporter: Kay Kim (Inactive) Assignee: Kay Kim (Inactive)
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
documents SERVER-32889 Mongo shell injection function for ha... Closed
Participants:
Days since reply: 5 years, 33 weeks, 6 days ago
Epic Link: DOCS: 4.0 Server

 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.



 Comments   
Comment by Githook User [ 21/Jun/18 ]

Author:

{'username': 'kay-kim', 'name': 'kay', 'email': 'kay.kim@10gen.com'}

Message: DOCS-11656: convertShardKeyToHashed.txt
Branch: master
https://github.com/mongodb/docs/commit/ed3e190efbefc8acd2b3f1568e2944fe2bb50e12

Generated at Thu Feb 08 08:03:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.