OPTIONS

Shards

A shard is a replica set or a single mongod that contains a subset of the data for the sharded cluster. Together, the cluster’s shards hold the entire data set for the cluster.

Typically each shard is a replica set. The replica set provides redundancy and high availability for the data in each shard.

Important

MongoDB shards data on a per collection basis. You must access all data in a sharded cluster via the mongos instances. If you connect directly to a shard, you will see only its fraction of the cluster’s data. There is no particular order to the data set on a specific shard. MongoDB does not guarantee that any two contiguous chunks will reside on a single shard.

Primary Shard

Every database has a “primary” [1] shard that holds all the un-sharded collections in that database.

Diagram of a primary shard. A primary shard contains non-sharded collections as well as chunks of documents from sharded collections. Shard A is the primary shard.

Diagram of a primary shard. A primary shard contains non-sharded collections as well as chunks of documents from sharded collections. Shard A is the primary shard.

To change the primary shard for a database, use the movePrimary command.

Warning

The movePrimary command can be expensive because it copies all non-sharded data to the new shard. During this time, this data will be unavailable for other operations.

When you deploy a new sharded cluster, the “first” shard becomes the primary shard for all existing databases before enabling sharding. Databases created subsequently may reside on any shard in the cluster.

[1]The term “primary” shard has nothing to do with the term primary in the context of replica sets.

Shard Status

Use the sh.status() method in the mongo shell to see an overview of the cluster. This reports includes which shard is primary for the database and the chunk distribution across the shards. See sh.status() method for more details.