Mongodb研究---Shard Keys(分片主键)

Shard Keys
The shard key determines the distribution of the collection’s documents among the cluster’s shards. The shard key is either an indexed field or indexed compound fields that exists in every document in the collection.
Shard key 决定了集合文档在集群上的分布。shard key 既可以是一个索引字段,也可以是一个组合索引字段。
Once you shard a collection, the shard key and the shard key values are immutable;
You cannot select a different shard key for that collection.
You cannot update the values of the shard key fields.

一旦你对一个集合做分片,那么shard key,shard key values是不变的
From the mongo shell connected to the mongos, use the sh.enableSharding() method to enable sharding on the target database. Enabling sharding on a database makes it possible to shard collections within a database.
sh.enableSharding("<database>")

该命令为数据开启分片
If the collection already contains data, you must create an index on the shard key using the db.collection.createIndex() method before using shardCollection().
如果该集合已经包含数据,那么在使用shardCollection 为集合分片之前,需要为shardkey创建索引
All sharded collections must have an index that supports the shard key; i.e. the index can be an index on the shard key or a compound index where the shard key is a prefix of the index
shard key的索引可以是单独的,也可以是某一个组合索引的最左
You cannot shard a collection that has unique indexes on other fields.
You cannot create unique indexes on other fields for a sharded collection.

集合分片不能有非shard key 的索引为唯一索引
sh.shardCollection( namespace, key )
The following operation shards the target collection using the ranged sharding strategy.
sh.shardCollection("<database>.<collection>", { <shard key> : <direction> } )

该操作为目标集合创建范围分片
The following operation shards the target collection using the hashed sharding strategy.
sh.shardCollection("<database>.<collection>", { <shard key> : "hashed" } )

该操作为目标集合创建hash分片

猜你喜欢

转载自blog.csdn.net/xf_87/article/details/80615893
今日推荐