Not able to shard collection

Posted on

Question :

I have enabled sharding using the below commands.

mongos> use news
switched to db news
mongos> sh.enableSharding("news");
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1633500027, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1633500026, 1)
}
mongos> db.createCollection("articles");
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1633500039, 2),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1633500039, 2)
}
mongos> sh.shardCollection("news.articles", {"dc": 1});
{
    "collectionsharded" : "news.articles",
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1633500077, 25),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1633500077, 21)
}
mongos> sh.status();
--- Sharding Status ---
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("615d26c277fa05dc0bf74e23")
  }
  shards:
        {  "_id" : "shard1rs",  "host" : "shard1rs/192.168.226.180:50001,192.168.226.180:50002,192.168.226.180:50003",  "state" : 1,  "topologyTime" : Timestamp(1633495862, 1),  "tags" : [ "bos" ] }
        {  "_id" : "shard2rs",  "host" : "shard2rs/192.168.226.180:50004,192.168.226.180:50005,192.168.226.180:50006",  "state" : 1,  "topologyTime" : Timestamp(1633496033, 2),  "tags" : [ "dfw" ] }
  active mongoses:
        "5.0.3" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                512 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
        {  "_id" : "news",  "primary" : "shard2rs",  "partitioned" : true,  "version" : {  "uuid" : UUID("dde4cff3-a2e4-4c81-87c1-4fd306370986"),  "timestamp" : Timestamp(1633500025, 2),  "lastMod" : 1 } }
                news.articles
                        shard key: { "dc" : 1 }
                        unique: false
                        balancing: true
                        chunks:

Even though, I have Sharded the collection using the above commands but getShardDistribution() is returning collection is not sharded.

mongos> db.articles.getShardDistribution();
Collection news.articles is not sharded.

Please let me know if I am missing something here.

Answer :

You need to add data to the collection. Only then the system can tell “how” data is shared. Before that, the collection is not sharded.

As workaround you can run this query:

db.getSiblingDB("config").chunks.aggregate([
   { $match: { ns: "news.articles" } },
   { $group: { _id: { shard: "$shard", ns: "$ns" }, chunks: { $sum: 1 } } },
   { $group: { _id: "$_id.ns", shards: { $push: { shard: "$_id.shard", chunks: "$chunks" } } } },
   { $unwind: "$shards" },
   { $group: { _id: "$_id", shards: { $push: { k: "$shards.shard", v: "$shards.chunks" } } } },
   { $replaceRoot: { newRoot: { $mergeObjects: [{ns:"$_id"}, { $arrayToObject: "$shards" }] } } },
   { $sort: { ns: 1 } }
])

Leave a Reply

Your email address will not be published. Required fields are marked *