Step 4: Add the remaining members to the replica set. Add the remaining members with the rs.add() method. 

rs.add("mongodb4.example.net") rs.add("mongodb5.example.net") 

## Step 5: Connect a mongo shell to the mongos . 

mongo mongodb6.example.net:27017/admin 

Step 6: Add the shard. In a mongo shell connected to the mongos , add the shard to the cluster with the sh.addShard() method: 

sh.addShard( "rs1/mongodb3.example.net:27017,mongodb4.example.net:27017,mongodb5.example.net:27017" 

## Shard a Collection 

## Step 1: Connect a mongo shell to the mongos . 

mongo mongodb6.example.net:27017/admin 

Step 2: Enable sharding for a database. Before you can shard a collection, you must first enable sharding for the collection’s database. Enabling sharding for a database does not redistribute data but makes it possible to shard the collections in that database. 

The following operation enables sharding on the test database: 

sh.enableSharding( "test" ) 

The operation returns the status of the operation: 

{ "ok" : 1 } 

Step 3: Determine the shard key. For the collection to shard, determine the shard key. The shard key (page 674) determines how MongoDB distributes the documents between shards. Good shard keys: 

• have values that are evenly distributed among all documents, 

• group documents that are often accessed at the same time into contiguous chunks, and 

• allow for effective distribution of activity among shards. 

Once you shard a collection with the specified shard key, you cannot change the shard key. For more information on shard keys, see Shard Keys (page 674) and Considerations for Selecting Shard Keys (page 694). 

This procedure will use the number field as the shard key for test_collection . 

Step 4: Create an index on the shard key. Before sharding a non-empty collection, create an index on the shard key (page 688). 

use test db.test_collection.createIndex( { number : 1 } ) 

