What is the syntax to state that a collection is to be sharded as location based, while using the range based sharding strategy. I know you have to use the the sh.shardCollection() command, but what arguments do I use in it?

2 Answers 2


As per MongoDB documentation here Ranged-based sharding involves dividing data into contiguous ranges determined by the shard key values. In this model, documents with “close” shard key values are likely to be in the same chunk or shard. This allows for efficient queries where reads target documents within a contiguous range. However, both read and write performance may decrease with poor shard key selection.

Ranged sharding is most efficient when the shard key displays the following traits:

For Example

A sharded cluster using the field X as the shard key. If the values for X have a large range, low frequency, and change at a non-monotonic rate, the distribution of inserts may look similar to the following:

enter image description here

Use the sh.shardCollection() method, specifying the full namespace of the collection and the target index or compound index to use as the shard key.

sh.shardCollection( "database.collection", { <shard key> } )

For further your ref here and here


Zones can help improve the locality of data for sharded clusters that span multiple data centers. A zone is a grouping of documents based on ranges of shard key values for a given sharded collection. In a balanced cluster, MongoDB directs reads and writes for a zone only to those shards inside that zone. You can associate each zone with one or more shards in the cluster. A shard can associate with any number of zones.

You can create zones of sharded data based on the shard key. You must use fields contained in the shard key when defining a new range for a zone to cover. For a compound shard key, the range must include the prefix of the shard key.

Reference: Zones in Sharded Clusters

A sharded cluster deployment has (a) Config server, (b) mongos router, and (c) Shard servers (shards). For a newly created cluster there will be no sharded databases or collections. From mongos you can see the current status of the cluster using the command sh.status().

From the question post:

What is the syntax to state that a collection is to be sharded as location based, while using the range based sharding strategy.

Create a Zone:

Add shards to a Zone. Associate a shard with a zone. A single shard may have multiple zones, and multiple shards may also have the same zone.

sh.addShardToZone("my_shard_name_1", "my_zone_name_1")

Do this for the shards and zones you are working with.

Create a Zone Range:

Associate a range of shard key values with a zone. Note the shard key is for a collection to be sharded. Any given shard key range can only have one assigned zone.

    { my_shard_key: "my_val_min" }, { my_shard_key: "my_val_max" },

Do this for the zones you are working with.

View Existing Zones:

Use sh.status() to see the zones associated to each shard in the cluster.

Use the following queries on the config database to view the shards associated with a zone or the keys range associated with a zone.

use config
db.shards.find({ tags: "my_zone_name_1" })
db.tags.find({ tag: "my_zone_name_1" })

Shard a Collection:

From this point, you can shard a database and distribute a collection's data as per the zones setup earlier. Assuming the database to be sharded is "db_test" and the collection is "coll_test" and the shard key is a field called as "my_shard_key" for the collection (the shard key field is the same as in the Create a Zone Range above):

use db_test
db.coll_test.createIndex( { my_shard_key: 1 } )
sh.shardCollection("db_test.coll_test", { my_shard_key: 1 } )

Verify the sharded collection with the command sh.status(). Further, you can add data to the sharded collection and perform query operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.