Create a Collection with PartitionKey in CosmosDB

爷,独闯天下 提交于 2020-01-02 07:46:04

问题


I want to create a Collection in Code using the DocumentClient like this

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

But how do I add a PartitionKey? In the CosmosDB Emulator I just add the name of the property on my class for the collection. Cant work it out. Could be I'm missing something fundamental here. Thank you


回答1:


You need to set 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 });


来源:https://stackoverflow.com/questions/48261296/create-a-collection-with-partitionkey-in-cosmosdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!