Database Reference
In-Depth Information
ferent shards. This is called a sharding strategy (read the previous chapter to learn more
about sharding keys and its different types).
Suppose the newdb database has a test collection with the following schema:
{
"_id": ObjectId("725c211a412f812548cv3258"),
"data": 1
}
We are going to enable sharding for this collection on the field _id . MongoDB will dis-
tribute the data across available shards with the _id field.
Simply by issuing the following command, you can enable sharding for a collection:
sh.shardCollection("database.collection", shard-keys)
But, before enabling sharding for a collection using the hash-based key strategy, you need
to create an index on the collection for the shard key using the following command:
db.test.ensureIndex( { _id : "hashed" } )
The result of issuing the preceding command would be as shown in the following screen-
shot:
Consequently, we can enable sharding for the collection and the _id field. In our ex-
ample, the command is as follows:
Search WWH ::




Custom Search