Lab: Shard a Collection need hints

I have used following commands for Shard a Collection lab:

show dbs
sh.enableSharding(“m103”)

use m103
db.products.count()
db.products.find().limit(1).pretty()

db.products.createIndex({ “sku”: “hashed” })
sh.shardCollection(“m103.products”, { “sku”: “hashed” })

sh.status()

However, got an error:
2 total, 1 passed, 0 skipped:
[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!
2 Likes

I do not think that you want a hashed shard key. The sku is not a monotonically increasing value.

1 Like

Hi @Baris_98176,

I hope you found @steevej-1495’s response helpful.

Let us know if you are still having any issues.

~ Shubham

What are the two correct fields, I feel like I’ve tried every possible permutation, sku and name (high card and low freq), or am I doing something foolish in the index creatio:

mongoimport --port 26000 -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin” --db=“m103” /dataset/products.json
mongo --port 26000 -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”
use m103
sh.enableSharding(“m103”)

db.products.createIndex( { “sku” : 1 } )
db.products.createIndex( { “name” : 1 } )
sh.shardCollection(“m103.products”, { “sku” : 1, “name”: 1 })

2 Likes

You do not have an index { sku : 1 , name : 1 } so you cannot use it to shard the collection.

Thanks - that solved it.
Previously I had the indexes but separately, and not as a compound index.