在 Cosmos DB 中写入数据时如何拥有自己的分区键

How to have your own partition key while writing data in Cosmos DB

我正在使用下面的代码在 Cosmos Db 中编写我的内容,但是在 Cosmos Db 中,我看到分区键是自动生成的,它是用于我保留为默认值的 Id。我的要求是拥有自己的分区键。从下面 json 我希望 TypeId 成为我的分区键 我如何在下面的代码中做到这一点?

content 属于 JObject 类型,格式如下 {{

"Classes": {
    "Subjects": {
      "Name": "testA",
      "TypeId": "AS88QW",
      "Public": "No"
    },
    "Sections": {
      "Scopes": true
    }
  }

C#代码:

// initialized database settings and read json file as string....
 var content = JsonConvert.DeserializeObject<JObject>( lines );

            var client = new DocumentClient( new Uri(Instance ), Key );
            await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(
                Database, Collection ),
                content);   
        }

您可以通过将多个 属性 值连接成一个人工分区键 属性。

来形成分区键

请按照以下页面中给出的步骤操作: https://www.c-sharpcorner.com/article/understanding-partitioning-and-partition-key-in-azure-cosmos-db/

创建容器时,您可以指定分区键,它是 JSON 文档中 属性 的路径。我的猜测是,当您(或其他人)创建容器时,/id 被选为分区键。

考虑到无法更改容器的分区键,您需要做的是创建一个新容器。创建新容器时,确保分区键 属性 设置为 /Classes/Subjects/TypeId.