create shardCollection show "couldn't find valid index for shard key" on no empty collection

179 views
Skip to first unread message

单眼皮

unread,
Feb 20, 2020, 3:51:17 AM2/20/20
to mongodb-user
Hello guys, I had shard database trade , and collections order, sub_order, order_receiver_district_list.
first I mad mongodb shard cluster, runcommand 
1) sh.enableSharding("trade")
2) sh.shardCollection( "trade.order", { tid: "hashed" } )
3)sh.shardCollection( "trade.order", { tid: "hashed" } )

Recently,I found order_receiver_district_list collection is datasize grow strong, so I runCommand shardCollection with mongoShell;
Im sure index exist in collection and print log;

mongos> db.order_receiver_district_list.getIndexes(); [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "trade.order_receiver_district_list" }, { "v" : 2, "key" : { "ayReceiverDistrictNumber" : 1 }, "name" : "idx_ayReceiverDistrictNumber", "ns" : "trade.order_receiver_district_list" }, { "v" : 2, "key" : { "ayStatus" : 1 }, "name" : "idx_ayStatus", "ns" : "trade.order_receiver_district_list" }, { "v" : 2, "key" : { "sellerId" : 1 }, "name" : "idx_sellerId", "ns" : "trade.order_receiver_district_list" }, { "v" : 2, "key" : { "sellerId" : 1, "tid" : 1, "storeId" : 1 }, "name" : "idx_seller_id_tid_store_id", "ns" : "trade.order_receiver_district_list" }, { "v" : 2, "unique" : true, "key" : { "tid" : 1, "storeId" : 1, "ayReceiverDistrictNumber" : 1 }, "name" : "idx_tid_storeId_ayReceiverDistrictNumber", "ns" : "trade.order_receiver_district_list" }, { "v" : 2, "key" : { "tid" : "hashed" }, "name" : "tid_hashed", "ns" : "trade.order_receiver_district_list" } ]

but response failed;
 sh.shardCollection("trade.order_receiver_district_list", {tid: "hashed"})
{
       
"ok" : 0,
       
"errmsg" : "couldn't find valid index for shard key",
       
"code" : 96,
       
"codeName" : "OperationFailed",
       
"operationTime" : Timestamp(1582178883, 97),
       
"$clusterTime" : {
               
"clusterTime" : Timestamp(1582178883, 97),
               
"signature" : {
                       
"hash" : BinData(0,"+GLqa0nYxEX4+ospdj1siAuV+YA="),
                       
"keyId" : NumberLong("6793149755456225311")
               
}
       
}
}

please help me , why doses it the cause of the tip?

Prasad Saya

unread,
Feb 20, 2020, 8:16:07 PM2/20/20
to mongodb-user
Hello,

sh.shardCollection( "trade.order", { tid: "hashed" } )

In the above code, the index should be on the collection order. Note, the index must be created (or exist) before running the  shardCollection command.
  • You can verify if the shard key index exists with this command: db.order.getIndexes()
  • In case the index is not there, you must create one before you run the sh.shardCollection()
  • You enable sharding on the database, but actually shard a collection.
Reference documentation: Enable Sharding

- Prasad.

单眼皮

unread,
Feb 20, 2020, 8:24:47 PM2/20/20
to mongod...@googlegroups.com
sorry, I want to collection order_receiver_district_list shardCollection and runcommand db.order_receiver_district_list.getIndexes() , but it’s failed


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/ac66f420-45f4-4e84-bc2a-c48dbae82159%40googlegroups.com.

单眼皮

unread,
Feb 20, 2020, 8:30:58 PM2/20/20
to mongodb-user
sh.shardCollection("trade.order_receiver_district_list", {tid: "hashed"})
I checked collection index and confirm there before running command, still show below error
Reply all
Reply to author
Forward
0 new messages