On this page本页内容
sh.
shardCollection
(namespace, key, unique, options)¶Shards a collection using the key
as a the shard key. The shard key determines how MongoDB distributes the collection’s documents among the shards.
The mongo
shell method sh.shardCollection
wraps the shardCollection
command.
sh.shardCollection()
takes the following arguments:
namespace |
string | The namespace of the collection to shard in the form "<database>.<collection>" . |
key |
document | The document that specifies the field or fields to use as the shard key.
Set the field value to either:
shard key must be supported by an index. Unless the collection is empty, the index must exist prior to the See also Shard Key Indexes |
unique |
boolean |
You cannot specify If specifying the |
options |
document | numInitialChunks and collation . |
The options
argument supports the following options:
numInitialChunks |
integer |
If the collection is not empty or the shard key does not contain a hashed field, the operation returns an error.
|
collation |
document | shardCollection has a default collation, you must include a collation document with { locale : "simple" } , or the shardCollection command fails. At least one of the indexes whose fields support the shard key pattern must have the simple collation. |
presplitHashedZones | boolean | Optional. Specify
|
Once a collection has been sharded, MongoDB provides no method to unshard a sharded collection.
Choosing the best shard key to effectively distribute load among your shards requires some planning.
_id
field).For more information, see Shard Keys.
Hashed shard keys use a hashed index or a compound hashed index as the shard key.
Use the form field: "hashed"
to specify a hashed shard key field.
Note
If chunk migrations are in progress while creating a hashed shard key collection, the initial chunk distribution may be uneven until the balancer automatically balances the collection.
See also参阅
The shard collection operation (i.e. shardCollection
command and the sh.shardCollection()
helper) can perform initial chunk creation and distribution for an empty or a non-existing collection if zones and zone ranges have been defined for the collection. Initial chunk distribution allows for a faster setup of zoned sharding. After the initial distribution, the balancer manages the chunk distribution going forward per usual.
See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for an example. If sharding a collection using a ranged or single-field hashed shard key, the numInitialChunks
option has no effect if zones and zone ranges have been defined for the empty collection.
To shard a collection using a compound hashed index, see Initial Chunk Distribution with Compound Hashed Indexes.
Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. When sharding an empty or non-existing collection using a compound hashed shard key, additional requirements apply in order for MongoDB to perform initial chunk creation and distribution.
The numInitialChunks
option has no effect if zones and zone ranges have been defined for the empty collection and
presplitHashedZones is false
.
See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for an example.
See also参阅
If specifying unique: true
:
sh.shardCollection()
creates the unique index on the shard key if such an index does not already exist.sh.shardCollection()
.Although you can have a unique compound index where the shard key is a prefix, if using unique
parameter, the collection must have a unique index that is on the shard key.
Changed in version 3.4.
If the collection has a default collation, the sh.shardCollection
command must include a collation
parameter with the value { locale: "simple" }
. For non-empty collections with a default collation, you must have at least one index with the simple collation whose fields support the shard key pattern.
You do not need to specify the collation
option for collections without a collation. If you do specify the collation option for a collection with no collation, it will have no effect.
mongos
uses "majority"
for the write concern of the shardCollection
command and its helper sh.shardCollection()
.
Given a collection named people
in a database named records
, the following command shards the collection by the zipcode
field:
The phonebook
database has a collection contacts
with no default collation. The following example uses sh.shardCollection()
to shard the phonebook.contacts
with:
last_name
field,5
initial chunks, andsimple
.