Lab: Shard a Collection

seems like when creating index on filed “sku” and “name” for collection product. MOngo is creating that index on Shrad2 and while running “RUN Test”. It is looking for indexes in Shrad1 and due to which when trying to shard collection . I am getting error “Please create an index that starts with the proposed shard key before sharding the collection”,

Step that I followed :slight_smile:

1)- mongos> db.m103.createIndex({“sku”:1})
{
“raw” : {
“shard2/localhost:27007,localhost:27008,localhost:27009” : {
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
},
“ok” : 1,
“operationTime” : Timestamp(1613611143, 8),
“$clusterTime” : {
“clusterTime” : Timestamp(1613611143, 8),
“signature” : {
“hash” : BinData(0,“NkGI04L2C1OPI4yW7sSyX6eMioc=”),
“keyId” : NumberLong(“6930403926550249502”)
}
}
2)-mongos> db.products.createIndex( { “name” : 1 } )
{
“raw” : {
“shard2/localhost:27007,localhost:27008,localhost:27009” : {
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
},
“ok” : 1,
“operationTime” : Timestamp(1613611165, 2),
“$clusterTime” : {
“clusterTime” : Timestamp(1613611165, 2),
“signature” : {
“hash” : BinData(0,“ZNUwczMYL4wCD34VIe5HRDLunrM=”),
“keyId” : NumberLong(“6930403926550249502”)
}
}
}

3)- sh.shardCollection(“m103.products”, { “sku” : 1, “name”: 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(1613611189, 4),
“$clusterTime” : {
“clusterTime” : Timestamp(1613611189, 4),
“signature” : {
“hash” : BinData(0,“jWu79ITRG348SDC0s6gdaLAwUJ4=”),
“keyId” : NumberLong(“6930403926550249502”)
}
}
}

Seems like this is a bug with IDE, not able to replicate the index on shard1???

I think the IDE is fine. You created the index sku on the collection m103 rather that the collection products.

Thanks Steevej for catch that typo :slight_smile:

But I am still receiving same error:

mongos> sh.enableSharding(“m103”)
{
“ok” : 1,
“operationTime” : Timestamp(1613622289, 2),
“$clusterTime” : {
“clusterTime” : Timestamp(1613622289, 2),
“signature” : {
“hash” : BinData(0,“fj9tYGUGf4WeCk585FuJ/awK330=”),
“keyId” : NumberLong(“6930453129695592477”)
}
}
}
mongos> db.products.createIndex({“sku”:1})
{
“raw” : {
“shard1/localhost:27001,localhost:27002,localhost:27003” : {
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
},
“ok” : 1,
“operationTime” : Timestamp(1613622301, 2),
“$clusterTime” : {
“clusterTime” : Timestamp(1613622301, 2),
“signature” : {
“hash” : BinData(0,“dRcjJlB9NPxBuH74C3kkEi75OQI=”),
“keyId” : NumberLong(“6930453129695592477”)
}
}
}
mongos> db.products.createIndex( { “name” : 1 } )
{
“raw” : {
“shard1/localhost:27001,localhost:27002,localhost:27003” : {
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 2,
“numIndexesAfter” : 3,
“ok” : 1
}
},
“ok” : 1,
“operationTime” : Timestamp(1613622318, 2),
“$clusterTime” : {
“clusterTime” : Timestamp(1613622318, 2),
“signature” : {
“hash” : BinData(0,“wSL48M1+IzW4Tdl8BHtjTcrLoRY=”),
“keyId” : NumberLong(“6930453129695592477”)
}
}
}
mongos> sh.shardCollection(“m103.products”, { “sku” : 1, “name”: 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(1613622332, 4),
“$clusterTime” : {
“clusterTime” : Timestamp(1613622332, 4),
“signature” : {
“hash” : BinData(0,“gkKyxK9Kw9B+hA7HOBPyYTFWOVU=”),
“keyId” : NumberLong(“6930453129695592477”)
}
}
}
mongos>

You do not have an index { “sku” : 1, “name”: 1 }.

You have an index { “sku” : 1 } and one { “name” : 1 }. You can shard on either one.