Home > Software design >  Cannot run commands like "shardCollection" or "enableSharding"
Cannot run commands like "shardCollection" or "enableSharding"

Time:03-30

I inherited a project to maintain. Apparently there was a decision to switch from CosmosDb to dedicated MongoDb. There is a project in a CI pipeline that seeds mongo database with collections and creates indexes. Now it is done with commands like that:

var partition = new Dictionary<string, object> {
    {"shardCollection", $"{databaseName}.{collectionName}"},
    { "key", new Dictionary<string, object>() { { $"{indexName}", "hashed" } } }
};
var command = new BsonDocumentCommand<BsonDocument>(partition);
var response = adminDb.RunCommand(command);

This does not work on my machine - I am getting Command shardCollection failes: no such command 'shardCollection'.

enter image description here

Any ideas what am I doing wrong? I found examples of that code elsewhere as a working code and I am very confused.

Update 1

I also tried to run this command before trying to set sharding on my collections:

var shardDbResult = adminDatabase.RunCommand<MongoDB.Bson.BsonDocument>(
    new MongoDB.Bson.BsonDocument() { 
        { "enableSharding", $"{databaseName}" } 
 });

but I also get a similar error Command enableSharding failes: no such command 'enableSharding'. I even tried that from mongoshenter image description here:

Update 2

So eventually I figured this might be something with my MongoDb installation. So I decided to start a sharded replica set in docker, which apparently I managed to run. Now the command:

db.adminCommand({enableSharding: "<databaseName>"})

works fine (at least it prints the report: ok: 1). Also this command works fine:

db.adminCommand({shardCollection: "<databaseName>.<collectionName>", key: {<fieldName>: 1}})

But my code from the top of the question continuously fails with the same message...

Versions:

  • OS Windows 10 Enterprise 21H2
  • .net 5 (5.0.15)
  • MongoDb 5.0
  • Mongo.Bson nuget 2.15.0
  • Mongo.Driver nuget 2.15.0
  • Mongo.DriverCore nuget 2.15.0

CodePudding user response:

I had a totally wrong set my replica set with sharding cluster. Once I used the instructions for deploying mongodb sharded cluster from this stackoverflow answer, my code started working.

  • Related