Error: Lab: Shard a Collection lab practice

Hi,

I am doing the lab ’ Lab: Shard a Collection’ I have imported the data, enabled sharding, created index and shard the collection but once I submit is it showing the below error.

[PASS] "The dataset is imported to m103.products"
[FAIL] "m103.products uses the correct shard key"
Did you shard the ‘m103.products’ collection using the correct shard key?
HINT: There are 2 document fields which are correct shard keys!

I created shard key on ‘sku’, ‘type,’ ‘name’ separately three times. below are the cmd what I executed.

mongo --port 26000 -u "m103-admin" -p "m103-pass" --authenticationDatabase admin 

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


sh.enableSharding("m103")
db.products.createIndex({"type":1})
sh.shardCollection("m103.products", {"type": 1})

Same I did for name & sku separately to try out if it is working or not but same error. can anyone please guide?

Hi @Souvik_Sardar,

Welcome to the MongoDB Community Forum,

As per your above command, type is not considered a good shard key because it doesn’t have much distinct value.

You can check out the by running the following command on m103.products:

db.products.distinct("type")

So, we are left with only two good shard keys.
After creating one you have to re-import the data to choose a different shard key.

I hope it helps!!

All the Best,
Kushagra

Hi @Kushagra_Kesav,

I initially selected sku as shard key(reason I know) but it was throwing error. then I tried with name as well but it is showing same error. FYI, for each and every try I dropped the collection and recreated

Hi @Souvik_Sardar,

Can you please share the screenshot of the IDE with the command and the error msg?
It will help us to resolve your issue.

Thanks
Kushagra

Hi @Kushagra_Kesav,

I am not sure why after clicking Run Test button 3 to 4 time it is showing the answer is correct. Since it was submitted successfully now, I can’t share the screenshot of the error. Thank you for reaching out. The issue is solved and I am proceeding with other lectures but if possible check once why it is showing error once we submit the solution for the first time.

Hi @Souvik_Sardar.

We are glad that your issue is resolved. Will surely check into this issue from our end.
Thanks for your feedback.

Happy Learning,
All the Best,
Kushagra

1 Like