Lab - Shard a Collection: Data only import to m103-repl-2 set

When conducting lab - Shard a Collection in Ch.3 Sharding, I had been raised up a question.

I had imported data onto the primary shard by executing following command:

mongoimport --drop /dataset/products.json --port 26000 -u “m103-admin”
-p “m103-pass” --authenticationDatabase “admin”
–db m103 --collection products

And products were successfully imported into mongos and m103-repl-2 set.

To verify data only imported into m103-repl-2 set, I had executed following command:

mongo --host “m103-repl-2/” -u m103-admin -p m103-pass admin
show dbs

m103 db was found

While executing following command:

mongo --host “m103-repl/” -u m103-admin -p m103-pass admin
show dbs

Are there any mechanism that data firstly imported to m103-repl-2 instead of m103-repl? Or it’s just random? Or I have mis-understood something?

Hi @Timothy_15966,

When you import data in a sharded environment, the mongos selects the Primary Shard for the newly created database (in your case m103). It is not random rather it’s based on factors such as the shard having the least amount of data.

Please note that you are connected to the mongos server here and not the Primary shard.

Hope it helps!

Please let me know if you still have any questions.

Shubham Ranjan
Curriculum Services Engineer

1 Like