Shard key can not exceed 128 characters ?

Shard key can not exceed 128 characters, if you try so Mongo doesn’t create an Index for that Shard key and as a result such sharded collection without index for sharding key will case performance degradation.

 

mongos> use sharding_test;
mongos> sh.enableSharding(“sharding_test”);

mongos> db.createCollection(‘test1’);

mongos> sh.shardCollection(“sharding_test.test1”, { ‘a1234567890’: 1, ‘b1234567890’: 1, ‘c1234567890’: 1 });
{ “collectionsharded” : “sharding_test.test1”, “ok” : 1 }
mongos> db.test1.insert({ ‘a1234567890’: 1, ‘b1234567890’: 1, ‘c1234567890’: 1 });
mongos> db.test1.getIndexes();
[
{
“v” : 1,
“key” : {
“_id” : 1
},
“ns” : “sharding_test.test1”,
“name” : “_id_”
},
{
“v” : 1,
“key” : {
“a1234567890” : 1,
“b1234567890” : 1,
“c1234567890” : 1
},
“ns” : “sharding_test.test1”,
“name” : “a1234567890_1_b1234567890_1_c1234567890_1”
}
]

mongos> db.createCollection(‘test2’);

mongos> sh.shardCollection(“sharding_test.test2”, { ‘a1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890’: 1, ‘b1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890’: 1, ‘c1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890’: 1 });
{ “collectionsharded” : “sharding_test.test2”, “ok” : 1 }
mongos> db.test2.insert({ “a1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890” : 1, “b1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890” : 1, “c1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890” : 1 });

mongos> db.test2.getIndexes();
[
{
“v” : 1,
“key” : {
“_id” : 1
},
“ns” : “sharding_test.test2”,
“name” : “_id_”
}
]

 

Mongo doesn’t create an index for sharding key if index name (constructed by mongo in background) exceeds 128 characters.

  • Ask Question