1.

What are the projection operators $, $elemMatch and $slice used for in MongoDB?

Answer»

MongoDB sharded CLUSTER has 3 components namely shards, mongos and config servers. We will deploy all components using the below process. 

  • Deploy shards as a replica set.

We need to start all the members of replica sets with the –shardsvr option.

mongod --replSet "RS0" --shardsvr  mongod --replSet "rs1" --shardsvr

Suppose we have 2 shards with 3-member replica set each, all 6 shards should be started with the above option. These shard members are deployed as replica sets on the host (h1, h2, h3…. h6) at port 27017.

sh1(M1, M2, M3 as replica set “rs0”) and sh2(M4, M5, M6 as replica set “rs1”)

  • Deploy Config Server Replica Set 

We need to start all members of config servers as a replica set with --configsvr

mongod --configsvr --replSet “cf1”

Config Sever (Member c1, c2 and c3 as a replica set cf1) on host H7, h8 at port 27017.

  • Deploy mongos.

Start the mongos specifying the config server replica set name followed by a SLASH / and at least one of the config server HOSTNAMES and ports. Mongos is deployed on server h9 at port 27017.

mongos --configdb cf1/h7:27017, h8:27017, h9:27017
  • Add all shard replica sets(rs0 and rs1) to the cluster with sh.addShard command from the mongos.
mongo h9:27017/admin sh.addShard( "rs0/h1:27017,h2:27017,h3:27017" ) sh.addShard( "rs1/h5:27017,h6:27017,h7:27017" )
  • At this point we have sharded cluster ready, we can check the status using sh.status command.
  • At last, we need to enable sharding at the database level, create an index on the shard key and shard a collection on the indexed shard key.
mongo h9:27017/admin sh.enableSharding( "test" ) use test db.test_collection.createIndex( { a : 1 } ) sh.shardCollection( "test.test_collection", { "a" : 1 } )


Discussion

No Comment Found