Skip to content

Commit ed3e190

Browse files
committed
DOCS-11656: convertShardKeyToHashed.txt
1 parent 0183947 commit ed3e190

File tree

7 files changed

+99
-6
lines changed

7 files changed

+99
-6
lines changed

source/core/hashed-sharding.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ Hashed Sharding
77

88
.. default-domain:: mongodb
99

10-
Hashed sharding uses a :ref:`hashed index <index-hashed-index>` of a
11-
single field as the :term:`shard key` to partition data across your
12-
sharded cluster.
10+
Hashed sharding uses a :ref:`hashed index <index-hashed-index>` to
11+
partition data across your shared cluster. Hashed indexes compute the
12+
hash value of a single field as the index value; this value is used as
13+
your shard key. [#hashvalue]_
1314

1415
.. include:: /images/sharding-hash-based.rst
1516

@@ -29,6 +30,12 @@ collection using the :dbcommand:`split` command.
2930

3031
.. include:: /includes/tip-applications-do-not-need-to-compute-hashes.rst
3132

33+
.. [#hashvalue]
34+
Starting in version 4.0, the :binary:`~bin.mongo` shell provides the
35+
method :method:`convertShardKeyToHashed()`. This method uses the
36+
same hashing function as the hashed index and can be used to see
37+
what the hashed value would be for a key.
38+
3239
.. _hashed-sharding-shard-key:
3340

3441
Hashed Sharding Shard Key

source/core/index-hashed.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ details.
3030
Hashing Function
3131
----------------
3232

33-
Hashed indexes use a hashing function to compute the
34-
hash of the value of the index field. The hashing function collapses
33+
Hashed indexes use a hashing function to compute the hash of the value
34+
of the index field. [#hashvalue]_ The hashing function collapses
3535
embedded documents and computes the hash for the entire value but does
3636
not support multi-key (i.e. arrays) indexes.
3737

3838
.. include:: /includes/tip-applications-do-not-need-to-compute-hashes.rst
3939

40+
.. [#hashvalue]
41+
Starting in version 4.0, the :binary:`~bin.mongo` shell provides the
42+
method :method:`convertShardKeyToHashed()`. This method uses the
43+
same hashing function as the hashed index and can be used to see
44+
what the hashed value would be for a key.
45+
4046
Create a Hashed Index
4147
---------------------
4248

source/includes/ref-toc-method-sh.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ description: "Internal. Waits for a change in ping state from one of the :binary
113113
name: ":method:`sh.updateZoneKeyRange()`"
114114
file: /reference/method/sh.updateZoneKeyRange
115115
description: "Associates a range of shard keys to a zone. Supports configuring :ref:`zones <zone-sharding>` in sharded clusters."
116+
---
117+
name: ":method:`convertShardKeyToHashed.txt()`"
118+
file: /reference/method/convertShardKeyToHashed
119+
description: "Returns the hashed value for the input."
116120
...

source/includes/warning-hashed-index-floating-point.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
point numbers that cannot be reliably converted to 64-bit
88
integers (and then back to floating point). MongoDB ``hashed`` indexes do
99
not support floating point values larger than 2\ :sup:`53`.
10+
11+
To see what the hashed value would be for a key, see
12+
:method:`convertShardKeyToHashed()`.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
=======================
2+
convertShardKeyToHashed
3+
=======================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Description
14+
-----------
15+
16+
.. method:: convertShardKeyToHashed(<document>)
17+
18+
Returns the hashed value for the input. The
19+
:method:`convertShardKeyToHashed()` method uses the same hashing
20+
function as the hashed index and can be used to see what the
21+
:doc:`hashed value </core/hashed-sharding>` would be for a key.
22+
23+
Example
24+
-------
25+
26+
Consider a sharded collection that uses a :doc:`hashed shard key
27+
</core/hashed-sharding>` [#populated]_
28+
29+
.. code-block:: javascript
30+
31+
use test
32+
33+
db.orders.createIndex( { _id: "hashed" } )
34+
35+
sh.shardCollection( "test.orders", { _id : "hashed" } )
36+
37+
If the following document exists in the collection, the hashed value of
38+
the ``_id`` field is used to distribute the document:
39+
40+
.. code-block:: javascript
41+
42+
{
43+
_id: ObjectId("5b2be413c06d924ab26ff9ca"),
44+
"item" : "Chocolates",
45+
"qty" : 25
46+
}
47+
48+
49+
To determine the hashed value of ``_id`` field used to distribute the
50+
document across the shards, you can use the
51+
:method:`convertShardKeyToHashed` method:
52+
53+
.. code-block:: javascrip
54+
55+
convertShardKeyToHashed( { _id: ObjectId("5b2be413c06d924ab26ff9ca") } )
56+
57+
.. [#populated]
58+
59+
If the collection already contains data, you must create a hashed
60+
index on the shard key before you shard the collection. For an empty
61+
collection, MongoDB creates the index as part of
62+
:method:`sh.shardCollection()`.
63+

source/release-notes/4.0.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ Miscellaneous
786786
:binary:`~bin.mongo` shell used the default ``MongoDB Shell`` value as the
787787
app name.
788788

789+
- Adds a :binary:`~bin.mongo` shell method
790+
:method:`convertShardKeyToHashed` to return the hashed value for a
791+
document.
792+
789793
Changes Affecting Compatibility
790794
-------------------------------
791795

source/tutorial/deploy-sharded-cluster-hashed-sharding.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ the :method:`sh.shardCollection()` method to shard a collection.
189189
If the collection already contains data, you must create a
190190
:ref:`index-type-hashed` on the :term:`shard key` using the
191191
:method:`db.collection.createIndex()` method before using
192-
:method:`~sh.shardCollection()`.
192+
:method:`~sh.shardCollection()`. [#hashvalue]_
193193

194194
If the collection is empty, MongoDB creates the index as part of
195195
:method:`sh.shardCollection()`.
@@ -209,3 +209,9 @@ The following operation shards the target collection using the
209209
such as :ref:`zones <zone-sharding>`. See the selection
210210
considerations listed in the :ref:`hashed-sharding-shard-key`.
211211

212+
.. [#hashvalue]
213+
Starting in version 4.0, the :binary:`~bin.mongo` shell provides the
214+
method :method:`convertShardKeyToHashed()`. This method uses the
215+
same hashing function as the hashed index and can be used to see
216+
what the hashed value would be for a key.
217+

0 commit comments

Comments
 (0)