定义
语法
$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") } ]
了解详情
有关聚合管道中的通用哈希,请参阅$hash 和$hexHash 。这些操作符与$toHashedIndexKey 的不同之处如下:
$toHashedIndexKey应用哈希索引语义。与$hash和$hexHash不同,它将null和缺失值哈希为Long数值,而不是返回null。
要了解详情,请参阅: