Deepak HG
Deepak HG

Reputation: 1

Unable to create the shard collection through .NET mongoDB Driver

Using MongoDB API, We are not able to create shard collection with custom Database names through .NET MongoDB Driver.

Command:

{"shardCollection", $"{MongoDatabase.DatabaseNamespace.DatabaseName}.{collectionName}"}
{"key", new BsonDocument {{"ShardKey1", "hashed"}}}

Getting below exception,

{"Command shardCollection failed: shardCollection must be run against the admin database."}

Able to create the shard collection only on database that are named as "admin".

  1. What exactly admin database mean here?
  2. Why admin database should not have custom names(In this case, If we give database name "admin" command worked)?
  3. Why facing issue in MongoDB API version 3.6 not with 3.2. What is the affected updates?

Upvotes: 0

Views: 761

Answers (2)

T. Nielsen
T. Nielsen

Reputation: 885

Appears after upgrade to .net mongodb driver 3.6+ semantics have changed a a bit, try this:

IMongoDatabase db;

var bson = new BsonDocument
{
    { "customAction", "CreateCollection" },
    { "collection", collectionName },
    { "shardKey", partitionKey },
    { "offerThroughput", 400 },
};

var shellCommand = new BsonDocumentCommand<BsonDocument>(bson);

db = connectionSet.MongoClient.GetDatabase(cosmosDbName);

var commandResult = db.RunCommand<BsonDocument>(shellCommand);

then depending on if you have autoscale you may need to specify this instead, have a look here https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/custom-commands

Upvotes: 0

Cane
Cane

Reputation: 199

The cosmos db seems to rather use a custom command for that. This works for me:

var createCollectionCommand = new BsonDocument
            {
                { "customAction", "CreateCollection" },
                { "collection", collection },
                { "shardKey", shardkey }
            };
await _db.RunCommandAsync<BsonDocument>(createCollectionCommand);

Upvotes: 0

Related Questions