在CosmosDB中使用PartitionKey创建一个集合 [英] Create a Collection with PartitionKey in CosmosDB

查看:63
本文介绍了在CosmosDB中使用PartitionKey创建一个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 }
);

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

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

推荐答案

您需要设置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 });

这篇关于在CosmosDB中使用PartitionKey创建一个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆