文档菜单
文档首页
/
MongoDB 手册
/ / /

$toHashedIndexKey(聚合)

本页内容

  • 定义
  • 语法
  • 示例
  • 了解更多
$toHashedIndexKey

计算并返回输入表达式的哈希值,使用与MongoDB创建散列索引相同的哈希函数。哈希函数将键或字符串映射到固定大小的数值。

注意

与散列索引不同,$toHashedIndexKey 聚合运算符不处理排序规则。这意味着运算符可能产生的哈希值与基于相同数据的散列索引不匹配。

$toHashedIndexKey 具有以下语法

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

您可以使用 $toHashedIndexKey 计算聚合管道中字符串的哈希值。此示例计算字符串 "string to hash" 的哈希值。

db.aggregate(
[
{ $documents: [ { val: "string to hash" } ] },
{ $addFields: { hashedVal: { $toHashedIndexKey: "$val" } } }
]
)

示例输出

[ { val: 'string to hash', hashedVal: Long("763543691661428748") } ]
  • convertShardKeyToHashed()

返回

$toDouble