Database Reference
In-Depth Information
sh.shardCollection("newdb.test", { "_id": "hashed" } )
The following screenshot shows the output of issuing this command:
Note
It's not necessary to enable sharding only for existing collections; you can enable sharding
for a collection that doesn't exist yet. MongoDB will create indexes on the collection us-
ing defined fields in the shardCollection() function.
If the collection already exists and contains data, you need to add indexes for the shard
keys using the ensureIndex() function first. The same approach should be used to
add a shard key with the hash-based method.
After enabling sharding on test collection, our sharding status is shown in the following
screenshot:
In order to test the sharding feature, we will insert data into the test collection in
newdb . As you know, the mongo shell is a JavaScript environment, and you can run the
JavaScript codes in this environment.
Using the following function, we insert the number 10,000 into the newdb database
and test collection:
Search WWH ::




Custom Search