简体   繁体   English

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

[英]Azure Cosmos MongoDB - Create collection with shard key

I need to create a collection in Azure's Cosmos MongoDB but with a partition/shard key as the required configuration is that the database will have a provisioned throughput (a set amount of RU's) and the collections in it will have a shared throughput.我需要在 Azure 的 Cosmos MongoDB 中创建一个集合,但使用分区/分片键,因为所需的配置是数据库将具有预配置的吞吐量(一组 RU),并且其中的集合将具有共享吞吐量。

The cluster's API is set to Cosmos DB Mongo API - If i create the collection in the cosmos i have no issues with Insert/Delete, but if i create the collection using the code below i will get an error saying {"Command insert failed: document does not contain shard key."} even though viewing the collection made in the portal and in the code look identical.集群的 API 设置为Cosmos DB Mongo API - 如果我在Cosmos DB Mongo API创建集合,则插入/删除没有问题,但如果我使用下面的代码创建集合,我将收到错误消息{"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();

I have talked to a microsoft representative but i get lacking "answers".我已经与微软代表谈过,但我缺乏“答案”。 He advised to use Mongo CSharp Driver but it seems this driver is unable to define a partition key (which makes sense).他建议使用Mongo CSharp Driver但该驱动程序似乎无法定义分区键(这是有道理的)。

How can i create a collection with a partition key?如何使用分区键创建集合?

Thank you.谢谢你。

You can use customCommand to create a collection with ShardKey.您可以使用customCommand通过ShardKey创建集合。 Something likes this:有点像这样:

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

I was dealing with the same thing.我正在处理同样的事情。 When using MongoDB csharp driver you should not call db.CreateCollection, but use sharding command instead.使用 MongoDB csharp 驱动程序时,不应调用 db.CreateCollection,而应使用分片命令。 This will create your unlimitted collection with the partition key for you.这将为您创建带有分区键的无限集合。

//Sharded collection must be initialized this way
var bson = new BsonDocument
{
    { "shardCollection", mongoClientProvider.DatabaseName + "." + CollectionName },
    { "key", new BsonDocument(ShardKeyName, "hashed") }
};

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

try
{
    var commandResult = database.RunCommand(shellCommand);
}
catch (MongoCommandException ex)
{
    logger.LogError(ex, ex.Result.ToString());
}

I guess you need to do that via command through the mongo shell我想你需要通过 mongo shell 通过命令来做到这一点

db.runCommand( { shardCollection: "yourCollection", key: { rateId: "_id" } } )

Refer here form more details请参阅here表格更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM