Azure Cosmos MongoDB - 使用分片键创建集合

Tom*_*ing 2 c# azure-cosmosdb azure-cosmosdb-mongoapi

我需要在 Azure 的 Cosmos MongoDB 中创建一个集合,但使用分区/分片键,因为所需的配置是数据库将具有预配置的吞吐量(一定量的 RU),并且其中的集合将具有共享吞吐量。

集群的 API 设置为Cosmos DB Mongo API- 如果我在 cosmos 中创建集合,则插入/删除没有任何问题,但如果我使用下面的代码创建集合,我会收到一条错误消息,指出{"Command insert failed: document does not contain shard key."}即使查看在门户中创建的集合,在代码中看起来相同。

client.CreateDocumentCollectionAsync(database.SelfLink,
new DocumentCollection
{
    Id = "CollectionName",
    PartitionKey = new PartitionKeyDefinition { Paths = new Collection<string> { "/_id" } }
}).Wait();
Run Code Online (Sandbox Code Playgroud)

我已经与微软代表交谈过,但我缺乏“答案”。他建议使用Mongo CSharp Driver,但似乎该驱动程序无法定义分区键(这是有道理的)。

如何使用分区键创建集合?

谢谢。

tha*_*cao 5

您可以使用customCommand通过 ShardKey 创建集合。有这样的东西:

var createCollectionCommand = new BsonDocument
                {
                    { "customAction", "CreateCollection" },
                    { "collection", "YourCollectionName" },
                    { "shardKey", "YourShardKey" }
                };
cosmosDatabase.RunCommand<BsonDocument>(createCollectionCommand);
Run Code Online (Sandbox Code Playgroud)