'Not able to create sharded collection

I have a mongodb Cluster with one config node(replication),one query node , and one shard node(with replication).

I tried to create sharded collection but it is giving me error:

command that i ran :

use exampleDB
sh.enableSharding("exampleDB")

Output:

{
    "ok" : 1,
    "operationTime" : Timestamp(1611723057, 4),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1611723057, 4),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

Now creating a collection :

 db.exampleCollection.ensureIndex( { _id : "hashed" } )

Output :

{
    "raw" : {
        "shardSet/clb1pidnmd3.ccsm.in:27017" : {
            "createdCollectionAutomatically" : true,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "commitQuorum" : "votingMembers",
            "ok" : 1
        }
    },
    "ok" : 1,
    "operationTime" : Timestamp(1611723093, 3),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1611723093, 3),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

Now sharding collection

sh.shardCollection( "exampleDB.exampleCollection", { "_id" : "hashed" } )

Output :

{
    "ok" : 0,
    "errmsg" : "Shard shard0000 not found",
    "code" : 70,
    "codeName" : "ShardNotFound",
    "operationTime" : Timestamp(1611723116, 5),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1611723116, 5),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

Not able to figure out what to do now



Solution 1:[1]

Before these steps, you need to have first added the shard servers through below command in Mongo.

sh.addShard("shard0000/IP:Port")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney