0

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.

3
  • Did you insert any data into this collection? Oct 6, 2021 at 20:22
  • Try db.adminCommand( { flushRouterConfig: "news.articles" } ); before you run getShardDistribution Oct 6, 2021 at 20:25
  • @WernfriedDomscheit I tried adding data to the collection but still the same result. Surprisingly, the same steps work with MongoDB 4.2.17 but not with 5.0.3
    – CrazyCoder
    Oct 7, 2021 at 8:13

2 Answers 2

0

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.

2
  • I tried adding data to the collection but still the same result.
    – CrazyCoder
    Oct 7, 2021 at 8:11
  • And your sh.status() still don't list any "chunks" under "databases"?
    – JJussi
    Oct 8, 2021 at 12:46
0

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 } }
])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.