Shard a collection lab issue

Trying to shard a collection using the following but keep saying I need to create the index on the key whcih I have already done so?

MongoDB Enterprise mongos> db.adminCommand( { shardCollection: “m103.products”, key: { “sku”: 1 } } )
{
“ok” : 0,
“errmsg” : “Please create an index that starts with the proposed shard key before sharding the collection”,
“code” : 72,
“codeName” : “InvalidOptions”,
“operationTime” : Timestamp(1545148863, 7),
“$clusterTime” : {
“clusterTime” : Timestamp(1545148863, 7),
“signature” : {
“hash” : BinData(0,“fnVZr6Mc472WQ3TfBse3+wN+bX0=”),
“keyId” : NumberLong(“6636014962433064987”)
}
}
}

Are you actually in the database that you’re sharding? :wink:

I was in the mongos? I thought that was were the sharding commands were run from?

I mean, you’re giving a command starting with “db.”. Did you “use” the database that you want to shard?

Otherwise it may not find the index. Perhaps…

1 Like

I created the index in the database/replication set where the m103.products is located.
I then ran the sharding command from both the mongos and also in the database itself and both give the same error.

Can you provide the output of the following:

mongos> use m103
mongos> db.products.getIndexes()

@Tess_Sluijter tried to make you realize that you did not created the index on the appropriate collection and you seem to be convinced that you did. The output of the above will convince us that you did or convince you that you did it on the wrong collection.

2 Likes