Reputation: 1
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".
Upvotes: 0
Views: 761
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
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