Mongodb – Where to insert data after sharding

mongodbsharding

  1. Actual database in localhost:30000 ————- I used this as Shard1
  2. another mongod in localhost:30001 ————- Shard2
  3. another mongod in localhost:30002 ————- Shard3

Added 1 config server and Router pointing the configdb

Now in Router,

1. mongo admin
2. sh.addShard("localhost:30000")..30002           //Added 3 shards successfully
3. sh.enableSharding("myDatabasename in shard1")  //Enabled successfully
4. sh.shardCollection(...)                       //Shard Collection successfully

I checked each shards and I am able to see that it is split-ed in ascending order like

shard key starting with A-D in shard2 as chunk 1
shard key starting with E-H in shard3 as chunk 1
shard key starting with I-O in shard1 as chunk 1
shard key starting with P in shard1 as chunk 2               //had upto P only

Now I need to insert a new record means in which mongo instance I have to insert?

I tried in shard1 and inserted a new record with sharding key starting with E, which is getting inserted in shard1 itself whereas it has to be inserted in shard3.

Best Answer

With a sharded cluster, you need to insert data via a mongos process. A mongos process acts as a query router: it directs queries/updates to the appropriate shard(s) based on the current cluster configuration and metadata. Typically you deploy one mongos per application server.

From your driver point of view, you can update your connect string and replace the address of your standalone server or replica set seed lists with one or more mongos servers to use.

For more information, you're best starting with the Sharding Introduction in the MongoDB manual.

Related Question