Skip to content

Commit 74350a9

Browse files
authored
DOCS-15574 documents $toHashedIndexKey agg stage (#4701)
* DOCS-15574 documents toHashedIndexKey agg stage * index page * internal review
1 parent 76f7c07 commit 74350a9

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

source/includes/extracts-agg-operators.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,6 @@ content: |
539539
- Description
540540
541541
* - :expression:`$getField`
542-
543542
- Returns the value of a specified field from a document.
544543
You can use :expression:`$getField` to retrieve the value of
545544
fields with names that contain periods (``.``) or start
@@ -551,12 +550,16 @@ content: |
551550
- Returns a random float between 0 and 1
552551
553552
* - :expression:`$sampleRate`
554-
555553
- Randomly select documents at a given rate. Although the exact
556554
number of documents selected varies on each run, the quantity
557555
chosen approximates the sample rate expressed as a percentage
558556
of the total number of documents.
559557
558+
* - :expression:`$toHashedIndexKey`
559+
- Computes and returns the hash of the input expression using
560+
the same hash function that MongoDB uses to create a hashed
561+
index.
562+
560563
---
561564
ref: agg-operators-objects
562565
content: |

source/reference/operator/aggregation.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,7 @@ Alphabetical Listing of Expression Operators
14751475
/reference/operator/aggregation/toDate
14761476
/reference/operator/aggregation/toDecimal
14771477
/reference/operator/aggregation/toDouble
1478+
/reference/operator/aggregation/toHashedIndexKey
14781479
/reference/operator/aggregation/toInt
14791480
/reference/operator/aggregation/toLong
14801481
/reference/operator/aggregation/toObjectId
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
===============================
2+
$toHashedIndexKey (aggregation)
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+
Definition
14+
----------
15+
16+
.. expression:: $toHashedIndexKey
17+
18+
Computes and returns the hash value of the input expression using
19+
the same hash function that MongoDB uses to create a hashed index.
20+
A hash function maps a key or string to a fixed-size numeric
21+
value.
22+
23+
Syntax
24+
------
25+
26+
``$toHashedIndexKey`` has the following syntax:
27+
28+
.. code-block:: javascript
29+
30+
{ $toHashedIndexKey: <key or string to hash> }
31+
32+
Example
33+
-------
34+
35+
You can use ``$toHashedIndexKey`` to compute the hashed value of a
36+
string in an aggregation pipeline. This example computes the hashed
37+
value of the string ``"string to hash"``:
38+
39+
.. code-block:: javascript
40+
:emphasize-lines: 4
41+
42+
db.aggregate(
43+
[
44+
{ $documents: [ { val: "string to hash" } ] },
45+
{ $addFields: { hashedVal: { $toHashedIndexKey: "$val" } } }
46+
]
47+
)
48+
49+
Example output:
50+
51+
.. code-block:: javascript
52+
:copyable: false
53+
54+
[ { val: 'string to hash', hashedVal: Long("763543691661428748") } ]
55+
56+
Learn More
57+
----------
58+
59+
- :method:`convertShardKeyToHashed()`

0 commit comments

Comments
 (0)