Docs Menu

Docs HomeMongoDB Manual

$toHashedIndexKey (aggregation)

On this page

  • Definition
  • Syntax
  • Example
  • Learn More

Computes and returns the hash value of the input expression using the same hash function that MongoDB uses to create a hashed index. A hash function maps a key or string to a fixed-size numeric value.

$toHashedIndexKey has the following syntax:

{ $toHashedIndexKey: <key or string to hash> }

You can use $toHashedIndexKey to compute the hashed value of a string in an aggregation pipeline. This example computes the hashed value of the string "string to hash":

{ $documents: [ { val: "string to hash" } ] },
{ $addFields: { hashedVal: { $toHashedIndexKey: "$val" } } }

Example output:

[ { val: 'string to hash', hashedVal: Long("763543691661428748") } ]
←  $toDouble(aggregation)$toInt (aggregation) →