在CosmosDB中使用PartitionKey创建一个集合

Lar*_*y R 3 azure-cosmosdb

我想像这样使用DocumentClient在代码中创建一个Collection

await client.CreateDocumentCollectionAsync (
   UriFactory.CreateDatabaseUri(databaseId),
   new DocumentCollection { Id = collectionId },
   new RequestOptions { OfferThroughput = 1000 }
);
Run Code Online (Sandbox Code Playgroud)

但是,我该如何添加PartitionKey?在CosmosDB仿真器中,我只是在类的集合中添加属性的名称。不能解决。可能是我在这里错过了一些基本的东西。谢谢

Ara*_* R. 5

你需要设置 DocumentCollection.PartitionKeyDefinition

DocumentCollection collectionDefinition = new DocumentCollection();
collectionDefinition.Id = collectionId;
collectionDefinition.PartitionKey.Paths.Add("/deviceId"); //--> add this

DocumentCollection partitionedCollection = await client.CreateDocumentCollectionAsync(
    UriFactory.CreateDatabaseUri(databaseId),
    collectionDefinition,
    new RequestOptions { OfferThroughput = 10100 });
Run Code Online (Sandbox Code Playgroud)