说明
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.
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
例子
use test db.orders.createIndex( { _id: "hashed" } ) sh.shardCollection( "test.orders", { _id : "hashed" } )
如果集合中存在以下文档,则使用_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:
convertShardKeyToHashed( ObjectId("5b2be413c06d924ab26ff9ca") )
| [1] | 如果集合已包含数据,则必须在对集合进行分片之前对分片键创建哈希索引。 对于空的collection,MongoDB 会将索引创建为sh.shardCollection()的一部分。 |