Docs HomeMongoDB Manual

convertShardKeyToHashed

On this page本页内容

Description描述

convertShardKeyToHashed(<Object>)

Returns the hashed value for the input. The convertShardKeyToHashed() method uses the same hashing function as the hashed index and can be used to see what the hashed value would be for a key.返回输入的哈希值。convertShardKeyToHashed()方法使用与哈希索引相同的哈希函数,可以用来查看键的哈希值

Example实例

Consider a sharded collection that uses a hashed shard key 考虑一个使用哈希分片键的分片集合[1]

use test

db.orders.createIndex( { _id: "hashed" } )

sh.shardCollection( "test.orders", { _id : "hashed" } )

If the following document exists in the collection, the hashed value of the _id field is used to distribute the document:如果集合中存在以下文档,则使用_id字段的哈希值来分发文档:

{
_id: ObjectId("5b2be413c06d924ab26ff9ca"),
"item" : "Chocolates",
"qty" : 25
}

To determine the hashed value of _id field used to distribute the document across the shards, you can use the convertShardKeyToHashed() method:要确定用于在分片之间分发文档的_id字段的哈希值,可以使用convertShardKeyToHashed()方法:

convertShardKeyToHashed( ObjectId("5b2be413c06d924ab26ff9ca") )
[1] If the collection already contains data, you must create a hashed index on the shard key before you shard the collection. 如果集合已经包含数据,则在对集合进行分片之前,必须在分片键上创建哈希索引。For an empty collection, MongoDB creates the index as part of sh.shardCollection().对于空集合,MongoDB将创建索引作为sh.shardCollection()的一部分。