My Mongo DB is sharded… now what?

Now that your mongodb cluster and shards of servers/replicasets are setup, in an architecture such as  https://sql2nosql.wordpress.com/2013/11/19/mongodb-cluster-architecture/ then it is time to define the sharding mechanism.
1. You can define sharding at the DB level using:
sh.enableSharding(“<database_name>”);
2.Then all you need to do is define sharding at the Collection level using:
sh.shardCollection(“<database_name>.<collection_name>, shard-key-pattern)
~~~~~~~~~~
Example.1 : Regular Shard
sh.shardCollection(“stats_db.counters”, { “metric_name” : 1, “metric_val” : 1 }
~~~~~~~~~~
Example.2 : Hashed Index

Sharding [Optional (from Mongo 2.4 onwards)] –

db.counters.ensureIndex( { "_id" : "hashed" } )
sh.shardCollection(“stats_db.counters”, { “metric_val” : 1, “_id” : 1 }
~~~~~~~~~~
These commands serve more as a reference, you should have thoroughly read through http://docs.mongodb.org/manual/sharding/ before attempting any of the above commands.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s