Not able to create shard key


I am working on the Lab - Shard a Collection. I have done everything as outlined, but I am not able to run the last command that will actually create the shard key. I have already created an index on the shard key field.

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(1556477334, 6),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1556477334, 6),
		"signature" : {
			"hash" : BinData(0,"fTZmuqkrTymSwo0Z3LDwpg3g95Q="),
			"keyId" : NumberLong("6683534394696663066")

Most likely you created index in a different DB (don’t forget to switch DB as appropriate for your step)
Please check your steps again

db.collection.getIndexes() will give index details

1 Like